Skip to content
This repository has been archived by the owner on Apr 9, 2020. It is now read-only.

Porting ss-redir to go #473

Open
wants to merge 13 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 12 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,4 @@
script/http
bin
.idea
.vscode/
5 changes: 3 additions & 2 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,9 @@ install:
- go get golang.org/x/crypto/cast5
- go get golang.org/x/crypto/salsa20
- go get github.com/aead/chacha20
- go install ./cmd/shadowsocks-local
- go install ./cmd/shadowsocks-server
- go get github.com/bonafideyan/shadowsocks-go/cmd/shadowsocks-local
- go get github.com/bonafideyan/shadowsocks-go/cmd/shadowsocks-server
- go get github.com/bonafideyan/shadowsocks-go/cmd/shadowsocks-httpget
script:
- PATH=$PATH:$HOME/gopath/bin bash -x ./script/test.sh
sudo: false
53 changes: 38 additions & 15 deletions cmd/shadowsocks-httpget/httpget.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package main
import (
"flag"
"fmt"
ss "github.com/shadowsocks/shadowsocks-go/shadowsocks"
"io"
"math"
"net"
Expand All @@ -14,6 +13,8 @@ import (
"strconv"
"strings"
"time"

ss "github.com/bonafideyan/shadowsocks-go/shadowsocks"
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fix your dependency.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

your can add your branch as a reference for git

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry, I don't know how to do it, could you please give me some steps in detail?

)

var config struct {
Expand Down Expand Up @@ -57,7 +58,11 @@ func get(connid int, uri, serverAddr string, rawAddr []byte, cipher *ss.Cipher,
}()
tr := &http.Transport{
Dial: func(_, _ string) (net.Conn, error) {
return ss.DialWithRawAddr(rawAddr, serverAddr, cipher.Copy())
if cipher != nil {
return ss.DialWithRawAddr(rawAddr, serverAddr, cipher.Copy())
}

return ss.DialAsClient(string(rawAddr), serverAddr)
},
}

Expand All @@ -77,8 +82,9 @@ func get(connid int, uri, serverAddr string, rawAddr []byte, cipher *ss.Cipher,
}

func main() {
flag.StringVar(&config.server, "s", "127.0.0.1", "server:port")
flag.IntVar(&config.port, "p", 0, "server:port")
server := flag.String("s", "127.0.0.1", "server:port")
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

keey the code style

proxy := flag.String("ss", "127.0.0.1", "proxy:port")
flag.IntVar(&config.port, "p", 0, "port")
flag.IntVar(&config.core, "core", 1, "number of CPU cores to use")
flag.StringVar(&config.passwd, "k", "", "password")
flag.StringVar(&config.method, "m", "", "encryption method, use empty string or rc4")
Expand All @@ -89,8 +95,17 @@ func main() {

flag.Parse()

if config.server == "" || config.port == 0 || config.passwd == "" || len(flag.Args()) != 1 {
fmt.Printf("Usage: %s -s <server> -p <port> -k <password> <url>\n", os.Args[0])
config.server = "127.0.0.1"
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

default config can be set with flag

var connectProxy bool
if *server != config.server {
config.server = *server
} else {
config.server = *proxy
connectProxy = true
}

if config.port == 0 || !connectProxy && config.passwd == "" || len(flag.Args()) != 1 {
fmt.Printf("Usage: %s -s[s] <server> -p <port> -k <password> <url>\n", os.Args[0])
os.Exit(1)
}

Expand All @@ -104,11 +119,6 @@ func main() {
uri = "http://" + uri
}

cipher, err := ss.NewCipher(config.method, config.passwd)
if err != nil {
fmt.Println("Error creating cipher:", err)
os.Exit(1)
}
serverAddr := net.JoinHostPort(config.server, strconv.Itoa(config.port))

parsedURL, err := url.Parse(uri)
Expand All @@ -122,10 +132,23 @@ func main() {
} else {
host = parsedURL.Host
}
// fmt.Println(host)
rawAddr, err := ss.RawAddr(host)
if err != nil {
panic("Error getting raw address.")

rawAddr := []byte(host)
var cipher *ss.Cipher
if !connectProxy {
if config.method == "" {
config.method = "aes-256-cfb"
}

cipher, err = ss.NewCipher(config.method, config.passwd)
if err != nil {
fmt.Println("Error creating cipher:", err)
os.Exit(1)
}
rawAddr, err = ss.RawAddr(host)
if err != nil {
panic("Error getting raw address.")
}
}

done := make(chan []time.Duration)
Expand Down
70 changes: 44 additions & 26 deletions cmd/shadowsocks-local/local.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,12 @@ import (
"strings"
"time"

ss "github.com/shadowsocks/shadowsocks-go/shadowsocks"
ss "github.com/bonafideyan/shadowsocks-go/shadowsocks"
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

pls fix the dependency

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done.

)

var debug ss.DebugLog

var (
debug ss.DebugLog

errAddrType = errors.New("socks addr type not supported")
errVer = errors.New("socks version not supported")
errMethod = errors.New("socks only support 1 method now")
Expand Down Expand Up @@ -246,6 +246,7 @@ func connectToServer(serverId int, rawaddr []byte, addr string) (remote *ss.Conn
}
return nil, err
}

debug.Printf("connected to %s via %s\n", addr, se.server)
servers.failCnt[serverId] = 0
return
Expand Down Expand Up @@ -280,7 +281,7 @@ func createServerConn(rawaddr []byte, addr string) (remote *ss.Conn, err error)
return nil, err
}

func handleConnection(conn net.Conn) {
func handleConnection(conn net.Conn, redir bool) {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

need a new function called redirectConnection

if debug {
debug.Printf("socks connect from %s\n", conn.RemoteAddr().String())
}
Expand All @@ -291,23 +292,37 @@ func handleConnection(conn net.Conn) {
}
}()

var err error = nil
if err = handShake(conn); err != nil {
log.Println("socks handshake:", err)
return
}
rawaddr, addr, err := getRequest(conn)
if err != nil {
log.Println("error getting request:", err)
return
}
// Sending connection established message immediately to client.
// This some round trip time for creating socks connection with the client.
// But if connection failed, the client will get connection reset error.
_, err = conn.Write([]byte{0x05, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00, 0x08, 0x43})
if err != nil {
debug.Println("send connection confirmation:", err)
return
var rawaddr, buf []byte
var addr string
var n int
if !redir {
var err error = nil
if err = handShake(conn); err != nil {
log.Println("socks handshake:", err)
return
}
rawaddr, addr, err = getRequest(conn)
if err != nil {
log.Println("error getting request:", err)
return
}
// Sending connection established message immediately to client.
// This some round trip time for creating socks connection with the client.
// But if connection failed, the client will get connection reset error.
_, err = conn.Write([]byte{0x05, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00, 0x08, 0x43})
if err != nil {
debug.Println("send connection confirmation:", err)
return
}
} else {
var err error
buf = ss.GetLeakyBuf()
n, err = conn.Read(buf)
if err != nil {
println(err.Error())
}

rawaddr, addr = ss.GetOriginalDst(&conn, string(buf[:n]))
}

remote, err := createServerConn(rawaddr, addr)
Expand All @@ -323,13 +338,13 @@ func handleConnection(conn net.Conn) {
}
}()

go ss.PipeThenClose(conn, remote, nil)
ss.PipeThenClose(remote, conn, nil)
go ss.PipeThenClose(conn, remote, nil, buf, n)
ss.PipeThenClose(remote, conn, nil, nil, 0)
closed = true
debug.Println("closed connection to", addr)
}

func run(listenAddr string) {
func run(listenAddr string, redir bool) {
ln, err := net.Listen("tcp", listenAddr)
if err != nil {
log.Fatal(err)
Expand All @@ -341,7 +356,7 @@ func run(listenAddr string) {
log.Println("accept:", err)
continue
}
go handleConnection(conn)
go handleConnection(conn, redir)
}
}

Expand Down Expand Up @@ -407,8 +422,10 @@ func main() {
var configFile, cmdServer, cmdURI string
var cmdConfig ss.Config
var printVer bool
var redirect bool

flag.BoolVar(&printVer, "version", false, "print version")
flag.BoolVar(&redirect, "redirect", false, "Redirect normal request to socks server.")
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

same

flag.StringVar(&configFile, "c", "config.json", "specify config file")
flag.StringVar(&cmdServer, "s", "", "server address")
flag.StringVar(&cmdConfig.LocalAddress, "b", "", "local address, listen only to this address if specified")
Expand Down Expand Up @@ -477,5 +494,6 @@ func main() {
}

parseServerConfig(config)
run(config.LocalAddress + ":" + strconv.Itoa(config.LocalPort))
run(config.LocalAddress+":"+strconv.Itoa(config.LocalPort), redirect)
}

12 changes: 6 additions & 6 deletions cmd/shadowsocks-server/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ import (
"syscall"
"time"

ss "github.com/shadowsocks/shadowsocks-go/shadowsocks"
ss "github.com/bonafideyan/shadowsocks-go/shadowsocks"
)

const (
Expand Down Expand Up @@ -169,12 +169,12 @@ func handleConnection(conn *ss.Conn, port string) {
go func() {
ss.PipeThenClose(conn, remote, func(Traffic int) {
passwdManager.addTraffic(port, Traffic)
})
}, nil, 0)
}()

ss.PipeThenClose(remote, conn, func(Traffic int) {
passwdManager.addTraffic(port, Traffic)
})
}, nil, 0)

closed = true
return
Expand Down Expand Up @@ -542,12 +542,12 @@ func managerDaemon(conn *net.UDPConn) {
res = handleAddPort(bytes.Trim(data[4:], "\x00\r\n "))
case strings.HasPrefix(command, "remove:"):
res = handleRemovePort(bytes.Trim(data[7:], "\x00\r\n "))
case strings.HasPrefix(command, "ping"):
conn.WriteToUDP(handlePing(), remote)
reportconnSet[remote.String()] = remote // append the host into the report list
case strings.HasPrefix(command, "ping-stop"): // add the stop ping command
conn.WriteToUDP(handlePing(), remote)
delete(reportconnSet, remote.String())
case strings.HasPrefix(command, "ping"):
conn.WriteToUDP(handlePing(), remote)
reportconnSet[remote.String()] = remote // append the host into the report list
}
if len(res) == 0 {
continue
Expand Down
Loading