Skip to content

Commit

Permalink
新增读取配置文件功能
Browse files Browse the repository at this point in the history
  • Loading branch information
wu committed Jan 12, 2021
1 parent 75d4327 commit 7440cd0
Show file tree
Hide file tree
Showing 7 changed files with 151 additions and 52 deletions.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# Created by .ignore support plugin (hsz.mobi)
.idea
bin
21 changes: 21 additions & 0 deletions conf/configure.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
{
"client": "192.168.2.235",
"server": "192.168.2.235",
"cipher": "192.168.2.235",
"key": "192.168.2.235",
"password": "192.168.2.235",
"keygen": 10,
"socks": "192.168.2.235",
"redir_tcp": "192.168.2.235",
"redir_tcp_6": "192.168.2.235",
"tcp_tun": "192.168.2.235",
"udp_tun": "192.168.2.235",
"udp_socks": true,
"udp": true,
"tcp": false,
"plugin": "192.168.2.235",
"plugin_opts": "192.168.2.235",
"verbose": true,
"udp_timeout": "1h",
"tcp_cork": false
}
69 changes: 69 additions & 0 deletions configure.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
package main

import (
"encoding/json"
"io/ioutil"
"os"
)

type Configure struct {
Client string `json:"client"`
Server string `json:"server"`
Cipher string `json:"cipher"`
Key string `json:"key"`
Password string `json:"password"`
Keygen int `json:"keygen"`
Socks string `json:"socks"`
RedirTCP string `json:"redir_tcp"`
RedirTCP6 string `json:"redir_tcp_6"`
TCPTun string `json:"tcp_tun"`
UDPTun string `json:"udp_tun"`
UDPSocks bool `json:"udp_socks"`
UDP bool `json:"udp"`
TCP bool `json:"tcp"`
Plugin string `json:"plugin"`
PluginOpts string `json:"plugin_opts"`
Verbose bool `json:"verbose"`
UDPTimeout Duration `json:"udp_timeout"`
TCPCork bool `json:"tcp_cork"`
}

var (
config Configure
)

func loadConfigure(filePath string) *Configure {

var (
content []byte
err error
)

if !exists(filePath) {
logf(filePath + " is not exits")
return nil
}

if content, err = ioutil.ReadFile(filePath); err != nil {
logf("Error reading configuration file, err is " + err.Error())
return nil
}

if err := json.Unmarshal(content, &config); err != nil {
logf("Error decoding json file, err is :" + err.Error())
return nil
}

return &config
}

func exists(path string) bool {
_, err := os.Stat(path)
if err != nil {
if os.IsExist(err) {
return true
}
return false
}
return true
}
10 changes: 10 additions & 0 deletions configure_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package main

import (
"testing"
)

func TestConfigure(t *testing.T) {
configure := loadConfigure("/home/wu/go/src/shadowsocks/conf/configure.json")
t.Log(configure.UDPTimeout.String())
}
36 changes: 36 additions & 0 deletions json.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package main

import (
"encoding/json"
"errors"
"time"
)

type Duration struct {
time.Duration
}

func (d Duration) MarshalJSON() ([]byte, error) {
return json.Marshal(d.String())
}

func (d *Duration) UnmarshalJSON(b []byte) error {
var v interface{}
if err := json.Unmarshal(b, &v); err != nil {
return err
}
switch value := v.(type) {
case float64:
d.Duration = time.Duration(value)
return nil
case string:
var err error
d.Duration, err = time.ParseDuration(value)
if err != nil {
return err
}
return nil
default:
return errors.New("invalid duration")
}
}
56 changes: 8 additions & 48 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,66 +5,26 @@ import (
"encoding/base64"
"flag"
"fmt"
"github.com/shadowsocks/go-shadowsocks2/core"
"github.com/shadowsocks/go-shadowsocks2/socks"
"io"
"log"
"net/url"
"os"
"os/signal"
"strings"
"syscall"
"time"

"github.com/shadowsocks/go-shadowsocks2/core"
"github.com/shadowsocks/go-shadowsocks2/socks"
)

var config struct {
Verbose bool
UDPTimeout time.Duration
TCPCork bool
}

func main() {

var flags struct {
Client string
Server string
Cipher string
Key string
Password string
Keygen int
Socks string
RedirTCP string
RedirTCP6 string
TCPTun string
UDPTun string
UDPSocks bool
UDP bool
TCP bool
Plugin string
PluginOpts string
}

flag.BoolVar(&config.Verbose, "verbose", false, "verbose mode")
flag.StringVar(&flags.Cipher, "cipher", "AEAD_CHACHA20_POLY1305", "available ciphers: "+strings.Join(core.ListCipher(), " "))
flag.StringVar(&flags.Key, "key", "", "base64url-encoded key (derive from password if empty)")
flag.IntVar(&flags.Keygen, "keygen", 0, "generate a base64url-encoded random key of given length in byte")
flag.StringVar(&flags.Password, "password", "", "password")
flag.StringVar(&flags.Server, "s", "", "server listen address or url")
flag.StringVar(&flags.Client, "c", "", "client connect address or url")
flag.StringVar(&flags.Socks, "socks", "", "(client-only) SOCKS listen address")
flag.BoolVar(&flags.UDPSocks, "u", false, "(client-only) Enable UDP support for SOCKS")
flag.StringVar(&flags.RedirTCP, "redir", "", "(client-only) redirect TCP from this address")
flag.StringVar(&flags.RedirTCP6, "redir6", "", "(client-only) redirect TCP IPv6 from this address")
flag.StringVar(&flags.TCPTun, "tcptun", "", "(client-only) TCP tunnel (laddr1=raddr1,laddr2=raddr2,...)")
flag.StringVar(&flags.UDPTun, "udptun", "", "(client-only) UDP tunnel (laddr1=raddr1,laddr2=raddr2,...)")
flag.StringVar(&flags.Plugin, "plugin", "", "Enable SIP003 plugin. (e.g., v2ray-plugin)")
flag.StringVar(&flags.PluginOpts, "plugin-opts", "", "Set SIP003 plugin options. (e.g., \"server;tls;host=mydomain.me\")")
flag.BoolVar(&flags.UDP, "udp", false, "(server-only) enable UDP support")
flag.BoolVar(&flags.TCP, "tcp", true, "(server-only) enable TCP support")
flag.BoolVar(&config.TCPCork, "tcpcork", false, "coalesce writing first few packets")
flag.DurationVar(&config.UDPTimeout, "udptimeout", 5*time.Minute, "UDP tunnel timeout")
var filePath = flag.String("path", "/etc/shadowsocks/conf.configure.json", "shadowsocks configure file path")
flag.Parse()
flags := loadConfigure(*filePath)

if flags == nil {
return
}

if flags.Keygen > 0 {
key := make([]byte, flags.Keygen)
Expand Down
8 changes: 4 additions & 4 deletions udp.go
Original file line number Diff line number Diff line change
Expand Up @@ -177,10 +177,10 @@ func udpRemote(addr string, shadow func(net.PacketConn) net.PacketConn) {
type natmap struct {
sync.RWMutex
m map[string]net.PacketConn
timeout time.Duration
timeout Duration
}

func newNATmap(timeout time.Duration) *natmap {
func newNATmap(timeout Duration) *natmap {
m := &natmap{}
m.m = make(map[string]net.PacketConn)
m.timeout = timeout
Expand Down Expand Up @@ -224,11 +224,11 @@ func (m *natmap) Add(peer net.Addr, dst, src net.PacketConn, role mode) {
}

// copy from src to dst at target with read timeout
func timedCopy(dst net.PacketConn, target net.Addr, src net.PacketConn, timeout time.Duration, role mode) error {
func timedCopy(dst net.PacketConn, target net.Addr, src net.PacketConn, timeout Duration, role mode) error {
buf := make([]byte, udpBufSize)

for {
src.SetReadDeadline(time.Now().Add(timeout))
src.SetReadDeadline(time.Now().Add(timeout.Duration))
n, raddr, err := src.ReadFrom(buf)
if err != nil {
return err
Expand Down

0 comments on commit 7440cd0

Please sign in to comment.