Skip to content

Commit c39d1c6

Browse files
author
Lukasz Zajaczkowski
committed
Add interconnection between clients
1 parent a000f77 commit c39d1c6

File tree

7 files changed

+61
-33
lines changed

7 files changed

+61
-33
lines changed

client.ini

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,9 @@
33
mode = client
44

55
[client]
6-
server = 104.199.19.152
6+
server = 104.199.15.195
77
# server port
88
port = 80
99
# MTU
10-
mtu = 1400
10+
mtu = 1400
11+
redirectGateway = true

server.ini

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,9 @@ mode = server
44

55
[server]
66
# port to listen
7-
port = 40100
7+
port = 8080
88
# server addr
99
vpnaddr = 10.1.1.1/24
10-
mtu = 1400
10+
mtu = 1400
11+
# allow communication between clients
12+
interconnection = false

utils/config.go

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -25,25 +25,27 @@ import (
2525

2626
// Server Config
2727
type ServerConfig struct {
28-
Port int
29-
ListenAddr string
30-
VpnAddr string
31-
MTU int
28+
Port int
29+
ListenAddr string
30+
VpnAddr string
31+
MTU int
32+
Interconnection bool
3233
}
3334

3435
// Client Config
3536
type ClientConfig struct {
36-
Server string
37-
Port int
38-
MTU int
37+
Server string
38+
Port int
39+
MTU int
40+
RedirectGateway bool
3941
}
4042

4143
type VpnConfig struct {
4244
Default struct {
43-
Mode string
44-
}
45-
Server ServerConfig
46-
Client ClientConfig
45+
Mode string
46+
}
47+
Server ServerConfig
48+
Client ClientConfig
4749
}
4850

4951
func ParseConfig(filename string) (interface{}, error) {

vendor/vendor.json

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
{
2+
"comment": "",
3+
"ignore": "test",
4+
"package": [],
5+
"rootPath": "github.com/zreigz/ws-vpn"
6+
}

vpn/client.go

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -151,7 +151,6 @@ func NewClient(cfg ClientConfig) error {
151151
}
152152

153153
}
154-
155154
return errors.New("Not expected to exit")
156155
}
157156

@@ -171,9 +170,11 @@ func (clt *Client) dispatcher(p []byte) {
171170
ipStr := string(message.Payload)
172171
ip, subnet, _ := net.ParseCIDR(ipStr)
173172
setTunIP(clt.iface, ip, subnet)
174-
err := redirectGateway(clt.iface.Name(), tun_peer.String())
175-
if err != nil {
176-
logger.Error("Redirect gateway error", err.Error())
173+
if clt.cfg.RedirectGateway {
174+
err := redirectGateway(clt.iface.Name(), tun_peer.String())
175+
if err != nil {
176+
logger.Error("Redirect gateway error", err.Error())
177+
}
177178
}
178179

179180
clt.state = STATE_CONNECTED

vpn/connection.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,8 @@ import (
2727

2828
const (
2929
writeWait = 10 * time.Second
30-
pongWait = 30 * time.Second
31-
pingPeriod = (pongWait) / 10
30+
pongWait = 60 * time.Second
31+
pingPeriod = (pongWait) / 2
3232
maxMessageSize = 1024 * 1024
3333
)
3434

vpn/server.go

Lines changed: 28 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -35,29 +35,29 @@ import (
3535

3636
type VpnServer struct {
3737
// config
38-
cfg ServerConfig
38+
cfg ServerConfig
3939
// interface
40-
iface *water.Interface
40+
iface *water.Interface
4141
// subnet
42-
ipnet *net.IPNet
42+
ipnet *net.IPNet
4343
// IP Pool
44-
ippool *VpnIpPool
44+
ippool *VpnIpPool
4545
// client peers, key is the mac address, value is a HopPeer record
4646

4747
// Registered clients
48-
clients map[string]*connection
48+
clients map[string]*connection
4949

5050
// Register requests
51-
register chan *connection
51+
register chan *connection
5252

5353
// Unregister requests
5454
unregister chan *connection
5555

56-
outData *Data
56+
outData *Data
5757

58-
inData chan *Data
58+
inData chan *Data
5959

60-
toIface chan []byte
60+
toIface chan []byte
6161
}
6262

6363
func NewServer(cfg ServerConfig) error {
@@ -176,24 +176,40 @@ func (srv *VpnServer) handleInterface() {
176176
break
177177
}
178178
header, _ := ipv4.ParseHeader(packet[:plen])
179-
logger.Debug("Sending to remote: ", header)
180-
179+
logger.Debug("Try sending: ", header)
181180
clientIP := header.Dst.String()
182181
client, ok := srv.clients[clientIP]
183182
if ok {
183+
if !srv.cfg.Interconnection {
184+
if srv.isConnectionBetweenClients(header) {
185+
logger.Info("Drop connection betwenn ", header.Src, header.Dst)
186+
continue
187+
}
188+
}
189+
184190
logger.Debug("Sending to client: ", client.ipAddress)
185191
client.data <- &Data{
186192
ConnectionState: STATE_CONNECTED,
187193
Payload: packet[:plen],
188194
}
195+
189196
} else {
190-
logger.Error("Client not found ", clientIP)
197+
logger.Warning("Client not found ", clientIP)
191198
}
192199

193200
}
194201
}()
195202
}
196203

204+
func (srv *VpnServer) isConnectionBetweenClients(header *ipv4.Header) bool {
205+
206+
if (header.Src.String() != header.Dst.String() && header.Src.String() != srv.ipnet.IP.String() && srv.ippool.subnet.Contains(header.Dst)) {
207+
return true
208+
}
209+
210+
return false
211+
}
212+
197213
func (srv *VpnServer) cleanUp() {
198214

199215
c := make(chan os.Signal, 1)

0 commit comments

Comments
 (0)