Gorilla Websocket with Web Proxy

161 views
Skip to first unread message

Jérémy Bricout

unread,
Apr 1, 2016, 10:34:36 AM4/1/16
to Gorilla web toolkit

Hello,


I'm working on a POC using Gorilla/Websocket to communicate, through a Web Proxy, with the Websocket Check Website "echo.websocket.org".

I'm using the free online Proxy "hide.me/en/" for testing.

When I simply try to communicate with "echo.websocket.org" (Server side), my client-site Websocket POC reach the response.

But when i try to add the Proxy gesture, everything goes wrong :(

Here is a sample of my code :


package main

import (
    "flag"
    "log"
    "net/url"
    "os"
    "os/signal"
    "time"

    "github.com/gorilla/websocket"
    "net/http"
)

var addrWebsocket = flag.String("addrWebsocket", "echo.websocket.org", "http service address")

func main() {
    flag.Parse()
    log.SetFlags(0)

    interrupt := make(chan os.Signal, 1)
    signal.Notify(interrupt, os.Interrupt)

    //Initialize the WebSocket URL and the Path to follow
    uWS := url.URL{Scheme: "wss", Host: *addrWebsocket}

    //Initialize the Proxy URL and the Path to follow
    uProxy, _ := url.Parse("https://hide.me/en/proxy")

    //Set the Dialer (especially the proxy)
    dialer := websocket.Dialer{
        Proxy: http.ProxyURL(uProxy),
    }
    //dialer := websocket.DefaultDialer ==> with this default dialer, it works !

    c, _, err := dialer.Dial(uWS.String(), nil) // ==> With the proxy config, it fails here !
    defer c.Close()

    done := make(chan struct{})

    go func() {
        defer c.Close()
        defer close(done)
        for {
            _, message, err := c.ReadMessage()
            if err != nil {
                log.Println("read:", err)
                return
            }
            log.Printf("recv: %s", message)
        }
    }()

    ticker := time.NewTicker(time.Second)
    defer ticker.Stop()

    for {
        select {
        case t := <-ticker.C:
            err := c.WriteMessage(websocket.TextMessage, []byte(t.String()))
            if err != nil {
                log.Println("write:", err)
                return
            }
        case <-interrupt:
            log.Println("interrupt")
            err := c.WriteMessage(websocket.CloseMessage, websocket.FormatCloseMessage(websocket.CloseNormalClosure, ""))
            if err != nil {
                log.Println("write close:", err)
                return
            }
                select {
                case <-done:
                case <-time.After(time.Second):
                }
            c.Close()
            return
        }
    }
}


I'm a very beginner in Go so don't hesitate to correct me if my code un not clear and clean.

Thanks for your help !

Message has been deleted

Jérémy Bricout

unread,
Apr 1, 2016, 1:57:26 PM4/1/16
to goril...@googlegroups.com

Hello, the error is a 400 bad request. Thanks for your help

Le 1 avr. 2016 7:41 PM, "Gary Burd" <gary...@gmail.com> a écrit :
What is the error returned from Dial? 

--
You received this message because you are subscribed to a topic in the Google Groups "Gorilla web toolkit" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/gorilla-web/cDDHodNRTCI/unsubscribe.
To unsubscribe from this group and all its topics, send an email to gorilla-web...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
Reply all
Reply to author
Forward
Message has been deleted
0 new messages