Skip to content

Commit

Permalink
Revert "1. try utp."
Browse files Browse the repository at this point in the history
This reverts commit d0b545b.
  • Loading branch information
dearplain committed May 19, 2020
1 parent d0b545b commit a70fe4a
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 424 deletions.
4 changes: 0 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,3 @@
# fast-shadowsocks

Try [utp](https://github.com/anacrolix/utp), a test here.

# shadowsocks-go

Current version: 1.1.4 [![Build Status](https://travis-ci.org/shadowsocks/shadowsocks-go.png?branch=master)](https://travis-ci.org/shadowsocks/shadowsocks-go)
Expand Down
52 changes: 2 additions & 50 deletions cmd/shadowsocks-local/local.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,19 +5,15 @@ import (
"errors"
"flag"
"fmt"
ss "github.com/shadowsocks/shadowsocks-go/shadowsocks"
"io"
"log"
"math/rand"
"net"
"os"
"path"
"strconv"
"sync"
"time"

"github.com/anacrolix/utp"
"github.com/dearplain/fast-shadowsocks/reusepipe"
ss "github.com/dearplain/fast-shadowsocks/shadowsocks"
)

var debug ss.DebugLog
Expand Down Expand Up @@ -232,40 +228,9 @@ func parseServerConfig(config *ss.Config) {
return
}

var pipes *reusepipe.Pipes
var serverAddr string
var pipesNeedUpdate bool
var pipeLock sync.Mutex

func DialWithRawAddr(rawaddr []byte, server string, cipher *ss.Cipher) (c *ss.Conn, err error) {
conn, err := pipes.NewPipe()
if err != nil {
pipesNeedUpdate = true
pipeLock.Lock()
if pipesNeedUpdate {
pipesNeedUpdate = false
conn, err := utp.Dial(serverAddr)
if err == nil {
pipes = reusepipe.NewPipes(conn)
log.Println("create new pipes ok to:", serverAddr)
} else {
log.Println("create new pipes failed:", err)
}
}
pipeLock.Unlock()
return
}
c = ss.NewConn(conn, cipher)
if _, err = c.Write(rawaddr); err != nil {
c.Close()
return nil, err
}
return
}

func connectToServer(serverId int, rawaddr []byte, addr string) (remote *ss.Conn, err error) {
se := servers.srvCipher[serverId]
remote, err = DialWithRawAddr(rawaddr, se.server, se.cipher.Copy())
remote, err = ss.DialWithRawAddr(rawaddr, se.server, se.cipher.Copy())
if err != nil {
log.Println("error connecting to shadowsocks server:", err)
const maxFailCnt = 30
Expand Down Expand Up @@ -444,19 +409,6 @@ func main() {
}
}

serverAddr = fmt.Sprintf("%s:%d", config.Server, config.ServerPort)
for {
conn, err := utp.Dial(serverAddr)
if err != nil {
log.Println("utp connect to ", serverAddr, "err:", err)
time.Sleep(2 * time.Second)
continue
}
log.Println("utp connect ok:", serverAddr)
pipes = reusepipe.NewPipes(conn)
break
}

parseServerConfig(config)

run(cmdLocal + ":" + strconv.Itoa(config.LocalPort))
Expand Down
45 changes: 18 additions & 27 deletions cmd/shadowsocks-server/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"errors"
"flag"
"fmt"
ss "github.com/shadowsocks/shadowsocks-go/shadowsocks"
"io"
"log"
"net"
Expand All @@ -14,10 +15,6 @@ import (
"strconv"
"sync"
"syscall"

"github.com/anacrolix/utp"
"github.com/dearplain/fast-shadowsocks/reusepipe"
ss "github.com/dearplain/fast-shadowsocks/shadowsocks"
)

var debug ss.DebugLog
Expand Down Expand Up @@ -258,7 +255,7 @@ func waitSignal() {
}

func run(port, password string) {
ln, err := utp.Listen(":" + port)
ln, err := net.Listen("tcp", ":"+port)
if err != nil {
log.Printf("error listening port %v: %v\n", port, err)
os.Exit(1)
Expand All @@ -267,29 +264,23 @@ func run(port, password string) {
var cipher *ss.Cipher
log.Printf("server listening port %v ...\n", port)
for {
conn, _ := ln.Accept()
pipes := reusepipe.NewPipes(conn)
go func(pipes *reusepipe.Pipes) {
for {
conn, err := pipes.Accept()
if err != nil {
// listener maybe closed to update password
debug.Printf("accept error: %v\n", err)
return
}
// Creating cipher upon first connection.
if cipher == nil {
log.Println("creating cipher for port:", port)
cipher, err = ss.NewCipher(config.Method, password)
if err != nil {
log.Printf("Error generating cipher for port: %s %v\n", port, err)
conn.Close()
continue
}
}
go handleConnection(ss.NewConn(conn, cipher.Copy()))
conn, err := ln.Accept()
if err != nil {
// listener maybe closed to update password
debug.Printf("accept error: %v\n", err)
return
}
// Creating cipher upon first connection.
if cipher == nil {
log.Println("creating cipher for port:", port)
cipher, err = ss.NewCipher(config.Method, password)
if err != nil {
log.Printf("Error generating cipher for port: %s %v\n", port, err)
conn.Close()
continue
}
}(pipes)
}
go handleConnection(ss.NewConn(conn, cipher.Copy()))
}
}

Expand Down
Loading

0 comments on commit a70fe4a

Please sign in to comment.