Skip to content

Commit

Permalink
First commit
Browse files Browse the repository at this point in the history
  • Loading branch information
Lukasz Zajaczkowski committed Jan 2, 2017
0 parents commit 57cdf60
Show file tree
Hide file tree
Showing 20 changed files with 1,225 additions and 0 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
# ws-vpn
10 changes: 10 additions & 0 deletions client.ini
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
[default]
# server or client
mode = client

[client]
server = 192.168.30.128
# server port
port = 40100
# MTU
mtu = 1400
Binary file added main
Binary file not shown.
76 changes: 76 additions & 0 deletions main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
/*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
* Author: Lukasz Zajaczkowski <[email protected]>
*
*/

package main

import (
"flag"
"os"
"runtime"

. "github.com/zreigz/ws-vpn/utils"
server "github.com/zreigz/ws-vpn/vpn"
client "github.com/zreigz/ws-vpn/vpn"
)

var debug bool
var cfgFile string


func main() {
flag.BoolVar(&debug, "debug", false, "Provide debug info")
flag.StringVar(&cfgFile, "config", "", "configfile")
flag.Parse()


InitLogger(debug)
logger := GetLogger()

checkerr := func(err error) {
if err != nil {
logger.Error(err.Error())
os.Exit(1)
}
}

if cfgFile == "" {
cfgFile = flag.Arg(0)
}

logger.Info("using config file: ", cfgFile)

icfg, err := ParseConfig(cfgFile)
logger.Debug(icfg)
checkerr(err)

maxProcs := runtime.GOMAXPROCS(0)
if maxProcs < 2 {
runtime.GOMAXPROCS(2)
}

switch cfg := icfg.(type) {
case ServerConfig:
err := server.NewServer(cfg)
checkerr(err)
case ClientConfig:
err := client.NewClient(cfg)
checkerr(err)
default:
logger.Error("Invalid config file")
}
}
10 changes: 10 additions & 0 deletions server.ini
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
[default]
# server or client
mode = server

[server]
# port range to listen
port = 40100
# server addr
vpnaddr = 10.1.1.1/24
mtu = 1400
64 changes: 64 additions & 0 deletions utils/config.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
/*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
* Author: Lukasz Zajaczkowski <[email protected]>
*
*/
package utils

import (
"errors"

"github.com/scalingdata/gcfg"
)

// Server Config
type ServerConfig struct {
Port int
ListenAddr string
VpnAddr string
MTU int
}

// Client Config
type ClientConfig struct {
Server string
Port int
MTU int
}

type VpnConfig struct {
Default struct {
Mode string
}
Server ServerConfig
Client ClientConfig
}

func ParseConfig(filename string) (interface{}, error) {
cfg := new(VpnConfig)
err := gcfg.ReadFileInto(cfg, filename)
if err != nil {
return nil, err
}
switch cfg.Default.Mode {
case "server":
return cfg.Server, nil
case "client":
return cfg.Client, nil
default:
return nil, errors.New("Wrong config data")
}
}

43 changes: 43 additions & 0 deletions utils/loging.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
/*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
* Author: Lukasz Zajaczkowski <[email protected]>
*
*/
package utils

import (
"os"

"github.com/op/go-logging"
)

var Logger = logging.MustGetLogger("ws-vpn")

func InitLogger(debug bool) {
fmt_string := "\r%{color}[%{time:06-01-02 15:04:05}][%{level:.6s}]%{color:reset} %{message}"
format := logging.MustStringFormatter(fmt_string)
logging.SetFormatter(format)
logging.SetBackend(logging.NewLogBackend(os.Stdout, "", 0))

if debug {
logging.SetLevel(logging.DEBUG, "ws-vpn")
} else {
logging.SetLevel(logging.INFO, "ws-vpn")
}
}

func GetLogger() *logging.Logger {
return Logger
}
1 change: 1 addition & 0 deletions vendor/github.com/gorilla/websocket
Submodule websocket added at 3ab3a8
1 change: 1 addition & 0 deletions vendor/github.com/op/go-logging
Submodule go-logging added at 970db5
1 change: 1 addition & 0 deletions vendor/github.com/scalingdata/gcfg
Submodule gcfg added at 37aaba
1 change: 1 addition & 0 deletions vendor/github.com/songgao/water
Submodule water added at 98078a
1 change: 1 addition & 0 deletions vendor/golang.org/x
Submodule x added at 45e771
Loading

0 comments on commit 57cdf60

Please sign in to comment.