Skip to content

Commit

Permalink
Multiple ports support in server.
Browse files Browse the repository at this point in the history
  • Loading branch information
cyfdecyf committed Dec 12, 2012
1 parent a31b319 commit 5715ecb
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 10 deletions.
27 changes: 18 additions & 9 deletions cmd/shadowsocks-server/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,9 @@ import (
"github.com/shadowsocks/shadowsocks-go/shadowsocks"
"log"
"net"
"strconv"
)

var config *shadowsocks.Config
var encTbl *shadowsocks.EncryptTable

func handleConnection(conn *shadowsocks.Conn) {
log.Printf("socks connect from %s\n", conn.RemoteAddr().String())
var err error = nil
Expand Down Expand Up @@ -97,12 +95,13 @@ func handleConnection(conn *shadowsocks.Conn) {

}

func run(port int) {
ln, err := net.Listen("tcp", fmt.Sprintf(":%d", port))
func run(port, password string) {
ln, err := net.Listen("tcp", ":"+port)
if err != nil {
log.Fatal(err)
}
log.Printf("starting server at port %d ...\n", port)
encTbl := shadowsocks.GetTable(password)
log.Printf("starting server at port %v ...\n", port)
for {
conn, err := ln.Accept()
if err != nil {
Expand All @@ -114,7 +113,17 @@ func run(port int) {
}

func main() {
config = shadowsocks.ParseConfig("config.json")
encTbl = shadowsocks.GetTable(config.Password)
run(config.ServerPort)
config := shadowsocks.ParseConfig("config.json")
if len(config.PortPassword) == 0 {
run(strconv.Itoa(config.ServerPort), config.Password)
} else {
if config.ServerPort != 0 {
log.Println("ignoring server_port and password option, only uses port_password")
}
c := make(chan byte)
for port, password := range config.PortPassword {
go run(port, password)
}
<-c // block forever
}
}
2 changes: 1 addition & 1 deletion config.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"server":"127.0.0.1",
"server_port":8388,
"local_port":1081,
"local_port":1080,
"password":"barfoo!",
"timeout":60
}

0 comments on commit 5715ecb

Please sign in to comment.