Example of Server and Client that communicate over HTTPS.
They're currently handle only /telemetry
endpoint, but using the example you can add another ones. Client sends some (fixed) data, server accept and log it, then response to the client with another (fixed) message.
Client features:
- DNS Name resolving using local resolver first, then bypass onto specified remote DNS.
- Granular timeouts on different operations, including TLS Handshake and zombie/idle connections.
- TCP Keep-Alive is enabled by "net/http" by default, but some related timeouts are set as well.
Server:
go get github.com/niki4/go-https-client-server
cd go-https-client-server
- You will need to obtain security certificate files (cert.pem, key.pem). If you have OpenSSL installed in your system, then you could use following command to generate self-signed certificate:
openssl req -x509 -nodes -days 365 -newkey rsa:2048 -keyout key.pem -out cert.pem
- You can enter any data on certificate fields, but Common Name - it must be your server name. For localhost it will be:
Common Name (e.g. server FQDN or YOUR name) []:localhost
go run server.go
Client:
Follow the same steps as for the server, but run client.go
. It's best to have server.go
running on the separate machine:
- Open
client.go
with some text editor and specify your server name in "baseURL" const, so it would likebaseURL = "nameyourserver.com"
. Save and close. go run client.go
##Known issues
- There's an issue with resolving
localhost
to 127.0.0.1 because of DNS returnsNXDOMAIN
flag instead of IP. As a bypass, for local testing you may keepbaseURL = "127.0.0.1"
client config. - Client has config
TLSClientConfig: &tls.Config{InsecureSkipVerify: true}
to allow accepting unsigned security certificates. As it's security lack, you should avoid using it on production, but it's ok for local testing. - Again, self-generated certificates are not intended for production, but if you look for running server somewhere on the internet, you may be interested in autocert package which helps your server to obtain Let's Encrypt certificate automatically.