Skip to content

Commit

Permalink
add config
Browse files Browse the repository at this point in the history
  • Loading branch information
clowwindy committed Nov 2, 2012
1 parent 15d6ae3 commit f45ea4f
Show file tree
Hide file tree
Showing 4 changed files with 55 additions and 9 deletions.
6 changes: 6 additions & 0 deletions config.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"Server":"127.0.0.1",
"ServerPort":8388,
"LocalPort":1080,
"Password":"barfoo!"
}
8 changes: 4 additions & 4 deletions src/local/local.go
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -88,8 +88,7 @@ func handleConnection(conn net.Conn, encryptTable, decryptTable []byte, server s
func run(encryptTable, decryptTable []byte, port int, server string) {
ln, err := net.Listen("tcp", fmt.Sprintf(":%d", port))
if err != nil {
log.Println(err)
return
log.Fatal(err)
}
log.Printf("starting server at port %d ...\n", port)
for {
Expand All @@ -103,7 +102,8 @@ func run(encryptTable, decryptTable []byte, port int, server string) {
}

func main() {
encyrptTable, decryptTable := shadowsocks.GetTable("foobar!")
run(encyrptTable, decryptTable, 1080, "127.0.0.1:8388")
config := shadowsocks.ParseConfig()
encyrptTable, decryptTable := shadowsocks.GetTable(config.Password)
run(encyrptTable, decryptTable, config.LocalPort, fmt.Sprintf("%s:%d", config.Server, config.ServerPort))

}
9 changes: 4 additions & 5 deletions src/server/server.go
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -101,8 +101,7 @@ func handleConnection(conn net.Conn, encryptTable, decryptTable []byte) {
func run(encryptTable, decryptTable []byte, port int) {
ln, err := net.Listen("tcp", fmt.Sprintf(":%d", port))
if err != nil {
log.Println(err)
return
log.Fatal(err)
}
log.Printf("starting server at port %d ...\n", port)
for {
Expand All @@ -116,7 +115,7 @@ func run(encryptTable, decryptTable []byte, port int) {
}

func main() {
encyrptTable, decryptTable := shadowsocks.GetTable("foobar!")
run(encyrptTable, decryptTable, 8388)

config := shadowsocks.ParseConfig()
encyrptTable, decryptTable := shadowsocks.GetTable(config.Password)
run(encyrptTable, decryptTable, config.ServerPort)
}
41 changes: 41 additions & 0 deletions src/shadowsocks/config.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
/**
* Created with IntelliJ IDEA.
* User: clowwindy
* Date: 12-11-2
* Time: 上午10:31
* To change this template use File | Settings | File Templates.
*/
package shadowsocks

import (
"encoding/json"
"os"
"log"
)
type Config struct {
Server string
ServerPort int
LocalPort int
Password string
}

func ParseConfig() Config {
file, err := os.Open("config.json") // For read access.
if err != nil {
log.Fatal("error opening config file config.json:", err)
}
data := make([]byte, 4096)
count, err := file.Read(data)
if err != nil {
log.Fatal("error reading config:", err)
}
if count == 4096 {
log.Fatal("config file is too large")
}
var config Config
err = json.Unmarshal(data[0:count], &config)
if err != nil {
log.Fatal("can not parse config:",err)
}
return config
}

0 comments on commit f45ea4f

Please sign in to comment.