func run(port, password string) { ln, err := net.Listen("tcp", ":"+port) if err != nil { log.Printf("error listening port %v: %v\n", port, err) os.Exit(1) } 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, password string) { var cipher *ss.Cipher port_i, _ := strconv.Atoi(port) log.Printf("listening udp port %v\n", port) conn, err := net.ListenUDP("udp", &net.UDPAddr{ IP: net.IPv6zero, Port: port_i, }) passwdManager.addUDP(port, password, conn) if err != nil { log.Printf("error listening udp port %v: %v\n", port, err) return } defer conn.Close() cipher, err = ss.NewCipher(config.Method, password) if err != nil { log.Printf("Error generating cipher for udp port: %s %v\n", port, err) conn.Close() } UDPConn := ss.NewUDPConn(conn, cipher.Copy()) for { UDPConn.ReadAndHandleUDPReq() } }