Skip to content

Commit

Permalink
feature: remove unuse feature
Browse files Browse the repository at this point in the history
- remove windows support
  • Loading branch information
ICKelin committed Feb 21, 2021
1 parent 49c7ce3 commit 4ed114a
Show file tree
Hide file tree
Showing 7 changed files with 110 additions and 246 deletions.
15 changes: 7 additions & 8 deletions Dockerfiles/gtund/gtund.conf
Original file line number Diff line number Diff line change
@@ -1,19 +1,18 @@
name="us-node-1-1"
istap=false

[server]
token="gtund-cs-goken"
listen=":9091"
listen=":9623"
auth_key="gtun-cs-token"
nameservers=["8.8.8.8", "8.8.4.4"]
route_url="http://www.ipdeny.com/ipblocks/data/countries/us.zone"
#route_url="http://www.ipdeny.com/ipblocks/data/countries/us.zone"

[dhcp]
cidr="100.64.240.1/24"
gateway="100.64.240.1"
nameserver="8.8.8.8"

[interface]
istap=false

[reverse]
rule="reverse.policy"
[log]
level="debug"
path="log.log"
days=3
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ require (
github.com/mmcloughlin/avo v0.0.0-20201216231306-039ef47f4f69 // indirect
github.com/pelletier/go-toml v1.8.1
github.com/pkg/errors v0.9.1
github.com/siddontang/go v0.0.0-20170517070808-cb568a3e5cc0
github.com/songgao/water v0.0.0-20200317203138-2b4b6d7c09d8
github.com/stretchr/testify v1.4.0
github.com/templexxx/cpufeat v0.0.0-20180724012125-cef66df7f161 // indirect
Expand Down
1 change: 1 addition & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,7 @@ github.com/prometheus/procfs v0.0.2/go.mod h1:TjEm7ze935MbeOT/UhFTIMYKhuLP4wbCsT
github.com/prometheus/procfs v0.1.3/go.mod h1:lV6e/gmhEcM9IjHGsFOCxxuZ+z1YqCvr4OA4YeYWdaU=
github.com/shiena/ansicolor v0.0.0-20151119151921-a422bbe96644 h1:X+yvsM2yrEktyI+b2qND5gpH8YhURn0k8OCaeRnkINo=
github.com/shiena/ansicolor v0.0.0-20151119151921-a422bbe96644/go.mod h1:nkxAfR/5quYxwPZhyDxgasBMnRtBZd0FCEpawpjMUFg=
github.com/siddontang/go v0.0.0-20170517070808-cb568a3e5cc0 h1:QIF48X1cihydXibm+4wfAc0r/qyPyuFiPFRNphdMpEE=
github.com/siddontang/go v0.0.0-20170517070808-cb568a3e5cc0/go.mod h1:3yhqj7WBBfRhbBlzyOC3gUxftwsU0u8gqevxwIHQpMw=
github.com/siddontang/goredis v0.0.0-20150324035039-760763f78400/go.mod h1:DDcKzU3qCuvj/tPnimWSsZZzvk9qvkvrIL5naVBPh5s=
github.com/siddontang/rdb v0.0.0-20150307021120-fc89ed2e418d/go.mod h1:AMEsy7v5z92TR1JKMkLLoaOQk++LVnOKL3ScbJ8GNGA=
Expand Down
233 changes: 8 additions & 225 deletions gtun/client.go
Original file line number Diff line number Diff line change
@@ -1,14 +1,10 @@
package gtun

import (
"bufio"
"encoding/json"
"fmt"
"net"
"net/http"
"os/exec"
"runtime"
"strings"
"sync"
"time"

Expand All @@ -30,15 +26,13 @@ var (
type ClientConfig struct {
ServerAddr string `toml:"server"`
AuthKey string `toml:"auth"`
layer2 bool `toml:"layer2"`
}

type Client struct {
serverAddr string
authKey string
myip string
gw string
layer2 bool
}

func NewClient(cfg *ClientConfig) *Client {
Expand All @@ -59,21 +53,13 @@ func NewClient(cfg *ClientConfig) *Client {
return &Client{
serverAddr: addr,
authKey: authkey,
layer2: cfg.layer2,
}
}

func (client *Client) Run() {
for {
server := client.serverAddr

if server == "" {
logs.Error("empty server")
time.Sleep(time.Second * 3)
continue
}

conn, err := conServer(server)
conn, err := net.DialTimeout("tcp", server, time.Second*10)
if err != nil {
logs.Error("connect to server fail: %v", err)
time.Sleep(time.Second * 3)
Expand All @@ -89,16 +75,14 @@ func (client *Client) Run() {

logs.Info("connect to %s success, assign ip %s", server, s2c.AccessIP)

ifce, err := NewIfce(client.layer2)
if err != nil {
logs.Error("new interface fail: %v", err)
continue
}

client.myip = s2c.AccessIP
client.gw = s2c.Gateway
sndqueue := make(chan []byte)
go ifaceRead(ifce, sndqueue)
ifce, err := NewIfce()
if err != nil {
logs.Error("new interface fail: %v", err)
return
}

err = setupIface(ifce, s2c.AccessIP, s2c.Gateway)
if err != nil {
Expand All @@ -108,48 +92,21 @@ func (client *Client) Run() {
}

done := make(chan struct{})

go func() {
failCount := 0
for {
routes, err := downloadRoutes(s2c.RouteScriptUrl)
if err != nil {
logs.Warn("download route from %s fail: %v", s2c.RouteScriptUrl, err)
failCount += 1
time.Sleep(time.Second * 3)
if failCount >= 10 {
break
}
continue
}

insertRoute(done, routes, s2c.AccessIP, s2c.Gateway, ifce.Name())
break
}
}()

wg := &sync.WaitGroup{}
wg.Add(3)

go ifaceRead(ifce, sndqueue)
go heartbeat(sndqueue, done, wg)
go snd(conn, sndqueue, done, wg)
go rcv(conn, ifce, wg)
wg.Wait()

setdownIface(ifce, s2c.AccessIP, s2c.Gateway)
ifce.Close()
logs.Info("reconnecting")
}
}

func conServer(srv string) (conn net.Conn, err error) {
tcp, err := net.DialTimeout("tcp", srv, time.Second*10)
if err != nil {
return nil, err
}

return tcp, nil
}

func authorize(conn net.Conn, key string) (s2cauthorize *common.S2CAuthorize, err error) {
c2sauthorize := &common.C2SAuthorize{
OS: common.OSID(runtime.GOOS),
Expand Down Expand Up @@ -259,177 +216,3 @@ func ifaceRead(ifce *water.Interface, sndqueue chan []byte) {
sndqueue <- bytes
}
}

func clearIfConfig(ifce *water.Interface, ip string, gw string) {
switch runtime.GOOS {
case "linux":
args := strings.Split(fmt.Sprintf("addr del %s/24 dev %s", ip, ifce.Name()), " ")
exec.Command("ip", args...).CombinedOutput()

case "darwin":

case "windows":
}
}

func setupIface(ifce *water.Interface, ip string, gw string) (err error) {
type CMD struct {
cmd string
args []string
}

cmdlist := make([]*CMD, 0)

switch runtime.GOOS {
case "linux":
cmdlist = append(cmdlist, &CMD{cmd: "ifconfig", args: []string{ifce.Name(), "up"}})
args := strings.Split(fmt.Sprintf("addr add %s/24 dev %s", ip, ifce.Name()), " ")
cmdlist = append(cmdlist, &CMD{cmd: "ip", args: args})

case "darwin":
cmdlist = append(cmdlist, &CMD{cmd: "ifconfig", args: []string{ifce.Name(), "up"}})

args := strings.Split(fmt.Sprintf("%s %s %s", ifce.Name(), ip, ip), " ")
cmdlist = append(cmdlist, &CMD{cmd: "ifconfig", args: args})

args = strings.Split(fmt.Sprintf("add -net %s/24 %s", gw, ip), " ")
cmdlist = append(cmdlist, &CMD{cmd: "route", args: args})

case "windows":
args := strings.Split(fmt.Sprintf("interface ip set address name=\"%s\" addr=%s source=static mask=255.255.255.0 gateway=%s", ifce.Name(), ip, gw), " ")
cmdlist = append(cmdlist, &CMD{cmd: "netsh", args: args})

args = strings.Split(fmt.Sprintf("delete 0.0.0.0 %s", gw), " ")
cmdlist = append(cmdlist, &CMD{cmd: "route", args: args})
}

for _, c := range cmdlist {
output, err := exec.Command(c.cmd, c.args...).CombinedOutput()
if err != nil {
return fmt.Errorf("run %s error %s", c, string(output))
}
}

return nil
}

func releaseDevice(device, ip, gateway string) (err error) {
type CMD struct {
cmd string
args []string
}

cmdlist := make([]*CMD, 0)

switch runtime.GOOS {
case "linux":
args := strings.Split(fmt.Sprintf("%s down", device), " ")
cmdlist = append(cmdlist, &CMD{cmd: "ifconfig", args: args})

case "darwin":
gw := strings.Split(gateway, ".")
if len(gw) != 4 {
break
}

s := strings.Join(gw[:3], ".")
args := strings.Split(fmt.Sprintf("delete -net %s/24 %s", s, ip), " ")
cmdlist = append(cmdlist, &CMD{cmd: "route", args: args})

args = strings.Split(fmt.Sprintf("%s delete %s", device, ip), " ")
cmdlist = append(cmdlist, &CMD{cmd: "ifconfig", args: args})

args = strings.Split(fmt.Sprintf("%s down", device), " ")
cmdlist = append(cmdlist, &CMD{cmd: "ifconfig", args: args})
}

for _, c := range cmdlist {
output, _ := exec.Command(c.cmd, c.args...).CombinedOutput()
if err != nil {
fmt.Printf("run %s error %s\n", c, string(output))
}
}

return nil
}

func downloadRoutes(url string) ([]string, error) {
routes := make([]string, 0)

logs.Info("downloading route file from: %s", url)
resp, err := http.Get(url)
if err != nil {
return nil, err
}
defer resp.Body.Close()

reader := bufio.NewReader(resp.Body)
for {
line, _, err := reader.ReadLine()
if err != nil {
break
}
// may need to validate ip/cidr format
routes = append(routes, string(line))
}
logs.Info("downloaded route file from: %s", url)
return routes, nil
}

func insertRoute(done chan struct{}, routedIPS []string, devIP, gw string, devName string) {
// Windows platform route add need iface index args.
ifceIndex := -1
ifce, err := net.InterfaceByName(devName)
if err != nil {
if runtime.GOOS == "windows" {
return
}
} else {
ifceIndex = ifce.Index
}

logs.Info("inserting routes")
for _, address := range routedIPS {
select {
case <-done:
return
default:
execRoute(address, devName, devIP, gw, ifceIndex)
}
}

logs.Info("inserted routes, routes count: %d", len(routedIPS))
}

type CMD struct {
cmd string
args []string
}

func execRoute(address, device, tunip, gateway string, ifceIndex int) {
cmd := &CMD{}

switch runtime.GOOS {
case "linux":
args := strings.Split(fmt.Sprintf("ro add %s dev %s", address, device), " ")
cmd = &CMD{cmd: "ip", args: args}

case "darwin":
args := strings.Split(fmt.Sprintf("add -net %s %s", address, tunip), " ")
cmd = &CMD{cmd: "route", args: args}

case "windows":
args := strings.Split(fmt.Sprintf("add %s %s if %d", address, gateway, ifceIndex), " ")
cmd = &CMD{cmd: "route", args: args}

default:
return
}

output, err := exec.Command(cmd.cmd, cmd.args...).CombinedOutput()
if err != nil {
logs.Debug("add %s fail %s", address, string(output))
}

logs.Debug(string(output))
}
15 changes: 8 additions & 7 deletions gtun/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,22 +2,23 @@ package gtun

import (
"io/ioutil"
"os"

"github.com/pelletier/go-toml"
)

type Config struct {
ClientConfig *ClientConfig `toml:"client"`
Log Log `toml:"log"`
}

func ParseConfig(path string) (*Config, error) {
fp, err := os.Open(path)
if err != nil {
return nil, err
}
type Log struct {
Days int64 `toml:"days"`
Level string `toml:"level"`
Path string `toml:"path"`
}

content, err := ioutil.ReadAll(fp)
func ParseConfig(path string) (*Config, error) {
content, err := ioutil.ReadFile(path)
if err != nil {
return nil, err
}
Expand Down
Loading

0 comments on commit 4ed114a

Please sign in to comment.