duanfuxing2417 2018-06-21 11:16
浏览 362

Golang中Caddy代理的WebSocket握手错误

I’m trying to initiate a websocket connection between chrome browser client and server.

Overview of my implementation : There are set of different up and running projects. The load balancer to all other projects and it handle all http requests, routes and proxy to other sub projects. These all projects use load balancers. My attempt is to create a websocket connection from chrom browser to one sub project.

caddy version : 0.9.3

websocket go library : github.com/gorilla/websocket 1

// Starting caddy server to server static files
args := []string{"bin/caddy", "--conf=" + commons.ServerConfigurations.CaddyFile, "-pidfile=bin/caddy.pid"}

if err := exec.Command("nohup", args...).Start(); err != nil {
    log.Fatalln("Error occourred while starting caddy server : ", err.Error())
    os.Exit(1)
}

//Starting the API server
router := routes.NewRouter()
http.Handle("/", router)

httpsServer := &http.Server{
    Addr: ":" + strconv.Itoa(commons.ServerConfigurations.HttpsPort+
        commons.ServerConfigurations.PortOffset),
    Handler:        logHandler,
    ReadTimeout:    time.Duration(commons.ServerConfigurations.ReadTimeOut) * time.Second,
    WriteTimeout:   time.Duration(commons.ServerConfigurations.WriteTimeOut) * time.Second,
    MaxHeaderBytes: 1 << 20,
}

httpsServer.ListenAndServeTLS(commons.ServerConfigurations.SSLCertificateFile,
        commons.ServerConfigurations.SSLKeyFile)

First when client reach to the domain, $DOMAIN_NAME such as www.sample.com. Caddy proxy the client request to the https://localhost:8107/analytics

https://{$DOMAIN_NAME}/analytics/ {
    tls pem_file key_file
    proxy /  https://localhost:8105/analytics { 
       websocket
       insecure_skip_verify
    }
}

There are multiple standalone servers. Following is the caddy configuration of server.It serve static files at the directory in webapps/analytics

localhost:8105/analytics {
    root  webapps/analytics
    gzip
    ext    .html
    tls pem_file key_file
    proxy  /api https://localhost:8108 {  HTTPS port:8108
         websocket
         insecure_skip_verify
    }
}

Inside the analytics sub project, " /api/ws " would trigger CreateSocketConnection() method.

//Starting the API server
    router := routes.NewRouter()
    http.Handle("/", router)
    http.HandleFunc("/api/ws", api.CreateSocketConnection)

CreateSocketConnection implementation :

func CreateSocketConnection(w http.ResponseWriter, r *http.Request) {
    var upgrader = websocket.Upgrader{
        ReadBufferSize:  1024,
        WriteBufferSize: 1024,
    }
    _, err = upgrader.Upgrade(w, r, nil)
    if err != nil {
        log.Fatal("upgrader failed :", err.Error())
    }
    //controllers.HandleSocket(ws)
}

Client side implementation :

conn = new WebSocket("wss://xxxx.com/analytics/api/ws");

Websocket upgrade error happen in golang backend server

2018/06/21 16:53:41 http: TLS handshake error from 127.0.0.1:47216: remote error: tls: bad certificate

How I use this secure https and websocket in same web server.Is it posible ?

web request : 'wss://xxxx.com/analytics/api/ws' failed: Error during WebSocket handshake: Unexpected response code: 502

Am I missing something in my implementation?

Thanks in advance

  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥15 基于卷积神经网络的声纹识别
    • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
    • ¥100 为什么这个恒流源电路不能恒流?
    • ¥15 有偿求跨组件数据流路径图
    • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
    • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
    • ¥15 CSAPPattacklab
    • ¥15 一直显示正在等待HID—ISP
    • ¥15 Python turtle 画图
    • ¥15 stm32开发clion时遇到的编译问题