func run(port, password string) { ln, err := net.Listen("tcp", ":"+port) if err != nil { log.Printf("error listening port %v: %v\n", port, err) return } passwdManager.add(port, password, ln) var cipher *ss.Cipher log.Printf("server listening port %v ...\n", port) for { 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 } } go handleConnection(ss.NewConn(conn, cipher.Copy())) } }
func get(connid int, uri, serverAddr string, rawAddr []byte, cipher ss.Cipher, done chan []time.Duration) { reqDone := 0 reqTime := make([]time.Duration, config.nreq) defer func() { done <- reqTime[:reqDone] }() tr := &http.Transport{ Dial: func(_, _ string) (net.Conn, error) { return ss.DialWithRawAddr(rawAddr, serverAddr, cipher.Copy()) }, } buf := make([]byte, 8192) client := &http.Client{Transport: tr} for ; reqDone < config.nreq; reqDone++ { start := time.Now() if err := doOneRequest(client, uri, buf); err != nil { return } reqTime[reqDone] = time.Now().Sub(start) if (reqDone+1)%1000 == 0 { fmt.Printf("conn %d finished %d get requests\n", connid, reqDone+1) } } }
func runUDP(port string, password [3]string) { addr, _ := net.ResolveUDPAddr(netUdp, ":"+port) conn, err := net.ListenUDP(netUdp, addr) if err != nil { log.Printf("error listening udp port %v: %v\n", port, err) return } passwdManager.addUDP(port, password, conn) log.Printf("server listening udp port %v ...\n", port) defer conn.Close() var cipher *ss.Cipher cipher, err = ss.NewCipher(config.Method, password[0]) if err != nil { log.Printf("Error generating cipher for udp port: %s %v\n", port, err) conn.Close() } ss.HandleUDPConnection(ss.NewUDPConn(conn, cipher.Copy()), password[1]) }
func run(port, password string) { var addr string var lastTime int64 ln, err := net.Listen("tcp", ":"+port) if err != nil { log.Printf("error listening port %v: %v\n", port, err) return } passwdManager.add(port, password, ln) var cipher *ss.Cipher log.Printf("server listening port %v ...\n", port) for { conn, err := ln.Accept() if err != nil { // listener maybe closed to update password debug.Printf("accept error: %v\n", err) return } newaddr := conn.RemoteAddr().String() nowtime := time.Now().Unix() if addr != newaddr && lastTime >= nowtime { conn.Close() continue } lastTime = nowtime addr = newaddr // 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())) } }
func run(port string) { ln, err := net.Listen("tcp", ":"+port) if err != nil { log.Printf("error listening port %v: %v\n", port, err) os.Exit(0) } var cipher *ss.Cipher log.Printf("server listening port %v ...\n", port) for { conn, err := ln.Accept() if err != nil { log.Printf("accept error: %v\n", err) continue } user, err := getUser(conn) if err != nil { log.Printf("Error get passeoed: %s %v\n", port, err) conn.Close() continue } size, err := storage.GetSize("flow:" + user.Name) if user.Limit < size { log.Printf("Error user runover: %s\n", size) conn.Close() continue } // log.Println("creating cipher for user:"******"Error generating cipher for user: %s %v\n", user.Name, err) conn.Close() continue } go handleConnection(user, ss.NewConn(conn, cipher.Copy())) } }