Skip to content

Commit b30c9a2

Browse files
author
Charles-Antoine Mathieu
committed
first code import
1 parent 1f50ae8 commit b30c9a2

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

69 files changed

+13973
-14
lines changed

.gitignore

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
*.log
2+
3+
ssl/
4+
*.key
5+
*.crt
6+
7+
wsp_server/wsp_server
8+
wsp_client/wsp_client
9+
test_api/test_api
10+
11+
# IntelliJ IDEA
12+
.idea

Godeps/Godeps.json

Lines changed: 24 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Godeps/Readme

Lines changed: 5 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

README.md

Lines changed: 27 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -28,16 +28,23 @@ WSP server configuration
2828
------------------------
2929

3030
```
31-
# wsp_server.conf.yml
31+
# wsp_server.cfg
3232
---
33-
host: 127.0.0.1
34-
port: 8080
35-
timeout : 1
33+
host : 127.0.0.1 # Address to bind the HTTP server
34+
port : 8080 # Port to bind the HTTP server
35+
timeout : 1000 # Time to wait before acquiring a WS connection to forward the request (milliseconds)
36+
blacklist : # Forbidden destination ( deny nothing if empty )
37+
- method : ".*" # Applied in order before whitelist
38+
url : "^http(s)?://google.*" # None must match
39+
whitelist : # Allowed destinations ( allow all if empty )
40+
- method : "^GET$" # Applied in order after blacklist
41+
url : "^http(s)?://.*" # One must match
42+
3643
```
3744

3845
```
39-
$ go build wsp_server.go
40-
$ ./wsp_server -config wsp_server.conf.yml
46+
$ cd wsp_client && go build
47+
$ ./wsp_server -config wsp_server.cfg
4148
{
4249
"Host": "127.0.0.1",
4350
"Port": 8080
@@ -63,13 +70,19 @@ WSP proxy configuration
6370
-----------------------
6471

6572
```
66-
# wsp_client.conf.yml
73+
# wsp_client.cfg
6774
---
68-
targets :
69-
- ws://127.0.0.1:8080/register
70-
poolMinSize : 10
71-
poolMinIdleSize : 5
72-
poolMaxSize : 100
75+
targets : # Endpoints to connect to
76+
- ws://127.0.0.1:8080/register #
77+
poolminsize : 10 # Default number of concurrent open (TCP) connections per WSP server
78+
poolminidlesize : 5 # Default number of concurrent open (TCP) connections to keep idle per WSP server
79+
poolmaxsize : 100 # Maximum number of concurrent open (TCP) connections per WSP server
80+
blacklist : # Forbidden destination ( deny nothing if empty )
81+
- method : ".*" # Applied in order before whitelist
82+
url : ".*forbidden.*" # None must match
83+
whitelist : # Allowed destinations ( allow all if empty )
84+
- method : "^GET$" # Applied in order after blacklist
85+
url : "^https://.*" # One must match
7386
```
7487

7588
- poolMinSize is the default number of opened TCP/HTTP/WS connections
@@ -83,8 +96,8 @@ poolMaxSize : 100
8396
the proxy will ever initiate per WSP server.
8497

8598
```
86-
$ go build wsp_client.go
87-
$ ./wsp_client -config wsp_client.conf.yml
99+
$ cd wsp_client && go build
100+
$ ./wsp_client -config wsp_client.cfg
88101
{
89102
"ID": "7e2d8782-f893-4ff3-7e9d-299b4c0a518a",
90103
"Targets": [

client/client.go

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
package client
2+
3+
import (
4+
"net/http"
5+
6+
"github.com/gorilla/websocket"
7+
)
8+
9+
// Client connects to one or more Server using HTTP websockets
10+
// The Server can then send HTTP requests to execute
11+
type Client struct {
12+
Config *Config
13+
14+
client *http.Client
15+
dialer *websocket.Dialer
16+
pools map[string]*Pool
17+
}
18+
19+
// NewClient creates a new Proxy
20+
func NewClient(config *Config) (c *Client) {
21+
c = new(Client)
22+
c.Config = config
23+
c.client = &http.Client{}
24+
c.dialer = &websocket.Dialer{}
25+
c.pools = make(map[string]*Pool)
26+
return
27+
}
28+
29+
// Start the Proxy
30+
func (c *Client) Start() {
31+
for _, target := range c.Config.Targets {
32+
pool := NewPool(c, target)
33+
c.pools[target] = pool
34+
go pool.Start()
35+
}
36+
}
37+
38+
// Shutdown the Proxy
39+
func (c *Client) Shutdown() {
40+
for _, pool := range c.pools {
41+
pool.Shutdown()
42+
}
43+
}

client/config.go

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
package client
2+
3+
import (
4+
"io/ioutil"
5+
6+
"github.com/nu7hatch/gouuid"
7+
"gopkg.in/yaml.v2"
8+
9+
"github.com/root-gg/wsp/common"
10+
)
11+
12+
// Config configures an Proxy
13+
type Config struct {
14+
ID string
15+
Targets []string
16+
PoolMinSize int
17+
PoolMinIdleSize int
18+
PoolMaxSize int
19+
Whitelist []*common.Rule
20+
Blacklist []*common.Rule
21+
}
22+
23+
// NewConfig creates a new ProxyConfig
24+
func NewConfig() (config *Config) {
25+
config = new(Config)
26+
27+
id, err := uuid.NewV4()
28+
if err != nil {
29+
panic(err)
30+
}
31+
config.ID = id.String()
32+
33+
config.Targets = []string{"ws://127.0.0.1:8080/register"}
34+
config.PoolMinSize = 10
35+
config.PoolMinIdleSize = 5
36+
config.PoolMaxSize = 100
37+
38+
config.Whitelist = make([]*common.Rule, 0)
39+
config.Blacklist = make([]*common.Rule, 0)
40+
41+
return
42+
}
43+
44+
// LoadConfiguration loads configuration from a YAML file
45+
func LoadConfiguration(path string) (config *Config, err error) {
46+
config = NewConfig()
47+
48+
bytes, err := ioutil.ReadFile(path)
49+
if err != nil {
50+
return
51+
}
52+
53+
err = yaml.Unmarshal(bytes, config)
54+
if err != nil {
55+
return
56+
}
57+
58+
// Compile the rules
59+
60+
for _, rule := range config.Whitelist {
61+
if err = rule.Compile(); err != nil {
62+
return
63+
}
64+
}
65+
66+
for _, rule := range config.Blacklist {
67+
if err = rule.Compile(); err != nil {
68+
return
69+
}
70+
}
71+
72+
return
73+
}

0 commit comments

Comments
 (0)