Skip to content

Commit 57cdf60

Browse files
author
Lukasz Zajaczkowski
committed
First commit
0 parents  commit 57cdf60

File tree

20 files changed

+1225
-0
lines changed

20 files changed

+1225
-0
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
# ws-vpn

client.ini

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
[default]
2+
# server or client
3+
mode = client
4+
5+
[client]
6+
server = 192.168.30.128
7+
# server port
8+
port = 40100
9+
# MTU
10+
mtu = 1400

main

6.84 MB
Binary file not shown.

main.go

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
/*
2+
* This program is free software: you can redistribute it and/or modify
3+
* it under the terms of the GNU General Public License as published by
4+
* the Free Software Foundation, either version 3 of the License, or
5+
* (at your option) any later version.
6+
*
7+
* This program is distributed in the hope that it will be useful,
8+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
9+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10+
* GNU General Public License for more details.
11+
*
12+
* You should have received a copy of the GNU General Public License
13+
* along with this program. If not, see <http://www.gnu.org/licenses/>.
14+
*
15+
* Author: Lukasz Zajaczkowski <[email protected]>
16+
*
17+
*/
18+
19+
package main
20+
21+
import (
22+
"flag"
23+
"os"
24+
"runtime"
25+
26+
. "github.com/zreigz/ws-vpn/utils"
27+
server "github.com/zreigz/ws-vpn/vpn"
28+
client "github.com/zreigz/ws-vpn/vpn"
29+
)
30+
31+
var debug bool
32+
var cfgFile string
33+
34+
35+
func main() {
36+
flag.BoolVar(&debug, "debug", false, "Provide debug info")
37+
flag.StringVar(&cfgFile, "config", "", "configfile")
38+
flag.Parse()
39+
40+
41+
InitLogger(debug)
42+
logger := GetLogger()
43+
44+
checkerr := func(err error) {
45+
if err != nil {
46+
logger.Error(err.Error())
47+
os.Exit(1)
48+
}
49+
}
50+
51+
if cfgFile == "" {
52+
cfgFile = flag.Arg(0)
53+
}
54+
55+
logger.Info("using config file: ", cfgFile)
56+
57+
icfg, err := ParseConfig(cfgFile)
58+
logger.Debug(icfg)
59+
checkerr(err)
60+
61+
maxProcs := runtime.GOMAXPROCS(0)
62+
if maxProcs < 2 {
63+
runtime.GOMAXPROCS(2)
64+
}
65+
66+
switch cfg := icfg.(type) {
67+
case ServerConfig:
68+
err := server.NewServer(cfg)
69+
checkerr(err)
70+
case ClientConfig:
71+
err := client.NewClient(cfg)
72+
checkerr(err)
73+
default:
74+
logger.Error("Invalid config file")
75+
}
76+
}

server.ini

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
[default]
2+
# server or client
3+
mode = server
4+
5+
[server]
6+
# port range to listen
7+
port = 40100
8+
# server addr
9+
vpnaddr = 10.1.1.1/24
10+
mtu = 1400

utils/config.go

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
/*
2+
* This program is free software: you can redistribute it and/or modify
3+
* it under the terms of the GNU General Public License as published by
4+
* the Free Software Foundation, either version 3 of the License, or
5+
* (at your option) any later version.
6+
*
7+
* This program is distributed in the hope that it will be useful,
8+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
9+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10+
* GNU General Public License for more details.
11+
*
12+
* You should have received a copy of the GNU General Public License
13+
* along with this program. If not, see <http://www.gnu.org/licenses/>.
14+
*
15+
* Author: Lukasz Zajaczkowski <[email protected]>
16+
*
17+
*/
18+
package utils
19+
20+
import (
21+
"errors"
22+
23+
"github.com/scalingdata/gcfg"
24+
)
25+
26+
// Server Config
27+
type ServerConfig struct {
28+
Port int
29+
ListenAddr string
30+
VpnAddr string
31+
MTU int
32+
}
33+
34+
// Client Config
35+
type ClientConfig struct {
36+
Server string
37+
Port int
38+
MTU int
39+
}
40+
41+
type VpnConfig struct {
42+
Default struct {
43+
Mode string
44+
}
45+
Server ServerConfig
46+
Client ClientConfig
47+
}
48+
49+
func ParseConfig(filename string) (interface{}, error) {
50+
cfg := new(VpnConfig)
51+
err := gcfg.ReadFileInto(cfg, filename)
52+
if err != nil {
53+
return nil, err
54+
}
55+
switch cfg.Default.Mode {
56+
case "server":
57+
return cfg.Server, nil
58+
case "client":
59+
return cfg.Client, nil
60+
default:
61+
return nil, errors.New("Wrong config data")
62+
}
63+
}
64+

utils/loging.go

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
/*
2+
* This program is free software: you can redistribute it and/or modify
3+
* it under the terms of the GNU General Public License as published by
4+
* the Free Software Foundation, either version 3 of the License, or
5+
* (at your option) any later version.
6+
*
7+
* This program is distributed in the hope that it will be useful,
8+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
9+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10+
* GNU General Public License for more details.
11+
*
12+
* You should have received a copy of the GNU General Public License
13+
* along with this program. If not, see <http://www.gnu.org/licenses/>.
14+
*
15+
* Author: Lukasz Zajaczkowski <[email protected]>
16+
*
17+
*/
18+
package utils
19+
20+
import (
21+
"os"
22+
23+
"github.com/op/go-logging"
24+
)
25+
26+
var Logger = logging.MustGetLogger("ws-vpn")
27+
28+
func InitLogger(debug bool) {
29+
fmt_string := "\r%{color}[%{time:06-01-02 15:04:05}][%{level:.6s}]%{color:reset} %{message}"
30+
format := logging.MustStringFormatter(fmt_string)
31+
logging.SetFormatter(format)
32+
logging.SetBackend(logging.NewLogBackend(os.Stdout, "", 0))
33+
34+
if debug {
35+
logging.SetLevel(logging.DEBUG, "ws-vpn")
36+
} else {
37+
logging.SetLevel(logging.INFO, "ws-vpn")
38+
}
39+
}
40+
41+
func GetLogger() *logging.Logger {
42+
return Logger
43+
}
Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

vendor/github.com/op/go-logging

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

vendor/github.com/scalingdata/gcfg

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

0 commit comments

Comments
 (0)