Golang : Forwarding a local port to a remote server example
Got a strange request yesterday. A friend who is an IT manager in his company needs to implement some control over his local network. He needs to block all the staffs(his co-workers) access to Facebook during working hours, but at the same time open up a "secret" door access to Facebook for his boss.
It is pretty trivial to configure the local network firewall to block access to certain websites nowadays. However, he prefers not to configure the "secret" door in the local network firewall. The next best solution is to implement a local port forwarding to remote server.
Below is a simple port forwarding solution in Golang that will initiate a bi-directional communication with a remote server(Facebook for example).
Here you go!
package main
import (
"io"
"log"
"net"
)
var localServerHost = "localhost:8880"
var remoteServerHost = "www.facebook.com:80"
func main() {
ln, err := net.Listen("tcp", localServerHost)
if err != nil {
log.Fatal(err)
}
log.Println("Port forwarding server up and listening on ", localServerHost)
for {
conn, err := ln.Accept()
if err != nil {
log.Fatal(err)
}
go handleConnection(conn)
}
}
func forward(src, dest net.Conn) {
defer src.Close()
defer dest.Close()
io.Copy(src, dest)
}
func handleConnection(c net.Conn) {
log.Println("Connection from : ", c.RemoteAddr())
remote, err := net.Dial("tcp", remoteServerHost)
if err != nil {
log.Fatal(err)
}
log.Println("Connected to ", remoteServerHost)
// go routines to initiate bi-directional communication for local server with a
// remote server
go forward(c, remote)
go forward(remote, c)
}
Sample output:
1) Run the program at the background and
2) On the browser, enter localhost:8880
in the address bar.
References:
https://www.socketloop.com/tutorials/golang-simple-client-server-example
See also : Golang : Simple client-server HMAC authentication without SSL example
By Adam Ng
IF you gain some knowledge or the information here solved your programming problem. Please consider donating to the less fortunate or some charities that you like. Apart from donation, planting trees, volunteering or reducing your carbon footprint will be great too.
Advertisement
Tutorials
+9.8k Golang : Use regular expression to get all upper case or lower case characters example
+13.6k Golang : convert(cast) string to float value
+7.6k Golang : How to feed or take banana with Gorilla Web Toolkit Session package
+4.4k Linux : sudo yum updates not working
+14.5k Golang : Adding XML attributes to xml data or use attribute to differentiate a common tag name
+6.8k Javascript : How to get JSON data from another website with JQuery or Ajax ?
+38.8k Golang : How to read CSV file
+39.6k Golang : Convert to io.ReadSeeker type
+12.6k Python : Convert IPv6 address to decimal and back to IPv6
+11.2k Golang : Handle API query by curl with Gorilla Queries example
+15.3k Golang : Convert date format and separator yyyy-mm-dd to dd-mm-yyyy
+9.4k Golang : Copy map(hash table) example