doufu1950 2017-04-26 05:27
浏览 88

TCP客户端无法正常工作golang

I have rest-ful interface written in golang. I need to do the authentication on each endpoint. once authentication is done I have to forward it back to a tcp server. I created a tcp client and any value that is coming from the channel is to be send to tcp server. the channel is populated from the http request body. The issue is that once I issue curl command the client is stuck with no response; so obviously I am doing something wrong not sure what is wrong. does anyone have any insights on what my problem might be?

 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+"
")
                message, _ := bufio.NewReader(conn).ReadString('
')
                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条回答 默认 最新

  • dtla92562 2017-04-26 14:41
    关注

    Your code execution blocks at c <- bodyString because nothing appears to be reading from that unbuffered channel. That line will pause execution until another routine reads from the channel.

    评论

报告相同问题?

悬赏问题

  • ¥500 火焰左右视图、视差(基于双目相机)
  • ¥100 set_link_state
  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本