func runWithCustomMethod(user user.User) { // port, password string, Cipher *ss.Cipher port := strconv.Itoa(user.GetPort()) // 检测端口是否已存在 _, ok := passwdManager.get(port) // 如果存在 if ok { return } password := user.GetPasswd() ln, err := net.Listen("tcp", ":"+port) if err != nil { Log.Error(fmt.Sprintf("error listening port %v: %v\n", port, err)) // os.Exit(1) return } passwdManager.add(port, password, ln) cipher, err, auth := user.GetCipher() if err != nil { return } Log.Info(fmt.Sprintf("server listening port %v ...\n", port)) for { conn, err := ln.Accept() if err != nil { // listener maybe closed to update password Log.Debug(fmt.Sprintf("accept error: %v\n", err)) return } // Creating cipher upon first connection. if cipher == nil { Log.Debug("creating cipher for port:", port) method := user.GetMethod() if strings.HasSuffix(method, "-auth") { method = method[:len(method)-5] auth = true } else { auth = false } cipher, err = ss.NewCipher(method, password) if err != nil { Log.Error(fmt.Sprintf("Error generating cipher for port: %s %v\n", port, err)) conn.Close() continue } } go handleConnection(user, ss.NewConn(conn, cipher.Copy()), auth) } }