package main import ( "fmt" "os" "github.com/inconshreveable/ngrok" ) func main() { // create a new ngrok client client, err := ngrok.NewClient(ngrok.ClientConfig{ AuthToken: os.Getenv("NGROK_AUTH_TOKEN"), }) if err != nil { fmt.Println(err) return } // start an http tunnel to localhost:8080 url, err := client.HTTP("http", "localhost:8080") if err != nil { fmt.Println(err) return } // print the public url fmt.Println(url) }
package main import ( "fmt" "net" "net/http" "github.com/inconshreveable/ngrok" ) func main() { // create a new ngrok client client, err := ngrok.NewClient(ngrok.ClientConfig{ AuthToken: "", }) if err != nil { fmt.Println(err) return } // start a tcp tunnel to localhost:22 addr, err := net.ResolveTCPAddr("tcp", "localhost:22") if err != nil { fmt.Println(err) return } _, err = client.DialTCP("tcp", addr) if err != nil { fmt.Println(err) return } // make an http request via the tunnel resp, err := http.Get("http://localhost:8080") if err != nil { fmt.Println(err) return } defer resp.Body.Close() // print the response body body, err := ioutil.ReadAll(resp.Body) if err != nil { fmt.Println(err) return } fmt.Println(string(body)) }In this example, we create a new ngrok client and start a tcp tunnel to a local ssh server running on port 22. We then make an http request via the tunnel to a local server running on port 8080. Lastly, we print out the response body of the http request. Package library: github.com/inconshreveable/ngrok