2017-04-26 93 views
-1

我有用golang编写的restful接口。我需要在每个端点上进行身份验证。一旦完成身份验证,我必须将其转发回tcp服务器。 我创建了一个tcp客户端,任何来自该通道的值都将被发送到tcp服务器。该通道从http请求主体填充。 问题是,一旦我发出curl命令,客户端就无法响应;所以显然我做错了什么事不知道错在哪里。有没有人对我的问题可能会有什么见解?在c <- bodyStringTCP客户端无法正常工作golang

package main 

      import (
       "bufio" 
       "encoding/json" 
       "flag" 
       "fmt" 
       "io/ioutil" 
       "log" 
       "net" 
       "net/http" 
       "net/http/httputil" 
       "net/url" 
       "os" 
       "strconv" 

       auth "github.com/abbot/go-http-auth" 
      ) 

      type Configuration struct { 
       Server  string 
       Port   int64 
       UserName  string 
       Pwd   string 
       Realm   string 
       ProxyPort  int64 
       Edeserver  string 

      } 

      var (
       Config *Configuration 
       logp = flag.Bool("log", false, "enable logging") 
      ) 

      func ReadConfiguration() { 
       file, _ := os.Open("Config.json") 
       decoder := json.NewDecoder(file) 
       Config = &Configuration{} 
       err := decoder.Decode(&Config) 
       if err != nil { 
        fmt.Println("error:", err) 
       } 

      } 

      func Secret(user, realm string) string { 
       if user == Config.UserName { 
        // password is "hello" 
        return Config.Pwd 
       } 
       return "" 
      } 

      func reverseProxyTows(w http.ResponseWriter, authenticatedRequest *auth.AuthenticatedRequest) { 
       req := &authenticatedRequest.Request 

       if *logp { 
        log.Println(" Authenticated Username ", authenticatedRequest.Username) 
        log.Println(" Authenticated URL ", req.URL.RequestURI()) 
       } 

       destinationURL := fmt.Sprintf("http://%s:%d", Config.Server, Config.Port) 
       u, err := url.Parse(destinationURL) 
       if err != nil { 
        log.Fatal(err) 
       } 

       if *logp { 
        log.Println("reverse_proxy", u) 
       } 

       reverseProxy := httputil.NewSingleHostReverseProxy(u) 

       reverseProxy.ServeHTTP(w, req) 
      } 

      func openConnectionTotcp(edechannel chan string) { 
       conn, _ := net.Dial("tcp", Config.Edeserver) 
       text := <-edechannel 
       fmt.Fprintf(conn, text+"\n") 
       message, _ := bufio.NewReader(conn).ReadString('\n') 
       fmt.Print("Message from server: " + message) 
      } 


      func main() { 
       ReadConfiguration() 
       flag.Parse() 
       c := make(chan string) 
       go openConnectionTotcp(c) 

       fmt.Printf("Started proxy to destination server %v:%d and is listening on %d ", Config.Server, Config.Port, Config.ProxyPort) 

       authenticator := auth.NewBasicAuthenticator(Config.Realm, Secret) 
       http.HandleFunc("/", authenticator.Wrap(reverseProxyTows)) 
       http.HandleFunc("/tyrion/1", authenticator.Wrap(func(w http.ResponseWriter, authenticatedRequest *auth.AuthenticatedRequest) { 
        req := &authenticatedRequest.Request 
        bodyBytes, err2 := ioutil.ReadAll(req.Body) 
        if err2 != nil { 
         log.Fatal(err2) 
        } 
        bodyString := string(bodyBytes) 
        c <- bodyString 
        fmt.Fprintf(w, "success") 
       })) 
       http.ListenAndServe(":"+strconv.FormatInt(Config.ProxyPort, 10), nil) 
      } 
+1

看起来好像你执行流被阻塞在“c < - bodyString”上。这可能发生在没有任何渠道消费者的情况下。请确定你在这里没有任何错误“conn,_:= net.Dial(”tcp“,Config.Edeserver)” –

+0

开始时,你不检查'net.Dial'中的错误或'ReadString',你没有关闭tcp连接,如果你想尝试读取多个消息,那么你就抛弃了'bufio.Reader',它可能有缓冲数据。你也只需要调用'tcp.Dial',然后退出goroutine,所以即使它工作,它也只能用于第一个http请求。 – JimB

回答

0

你执行代码块,因为没有什么似乎从无缓冲通道读取。该行将暂停执行,直到另一个例程从该通道读取。

+0

nope不是真正的openConnectionTotcp有一个chan参数,并且去例行程序读取它'''func openConnectionTotcp(edechannel chan string)conn,_:= net.Dial(“tcp”,Config.Edeserver) text: = <-edechannel fmt.Fprintf(conn,text +“\ n”) message,_:= bufio.NewReader(conn).ReadString('\ n') fmt.Print(“Message from server:”+ message ) } ''' – harry

+0

该例程将消耗一条消息,然后返回,之后对该通道的任何写入都将永远阻塞。你也抛弃了tcp.Dial的错误,所以谁知道那个例程中发生了什么。 – Adrian

+0

由于我运行的是本地tcp服务器,因此tcp dial工作正常,我确实在tcp服务器端看到了消息。做卷曲请求的客户端挂起。那么这个例程会消耗一条消息,然后返回,'它会返回,因为我并不是首先关闭通道。“ – harry