SSL socket listener

126 views
Skip to first unread message

Wesley Peng

unread,
Jun 3, 2020, 3:20:12 AM6/3/20
to golan...@googlegroups.com
Hello,

How do I program with SSL to make a server listen on specific port which accepts SSL transfer only?

Is there any guide for this since I have no experience on SSL socket programming.

Thanks.

Wesley Peng
wesle...@aol.com

Dimas Prawira

unread,
Jun 4, 2020, 12:54:47 AM6/4/20
to Wesley Peng, golan...@googlegroups.com
Here is an example running server with TLS 

package main

import (
    "net/http"
    "log"
)

func HelloServer(w http.ResponseWriter, req *http.Request) {
    w.Header().Set("Content-Type", "text/plain")
    w.Write([]byte("This is an example server.\n"))
}

func main() {
    http.HandleFunc("/hello", HelloServer)
    err := http.ListenAndServeTLS(":443", "server.crt", "server.key", nil)
    if err != nil {
        log.Fatal("ListenAndServe: ", err)
    }
}

so in http package there is "ListenAndServeTLS" which can be used to run the server with TLS enabled. 

Hope that helps



--
You received this message because you are subscribed to the Google Groups "golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/golang-nuts/1690752345.1320667.1591168756241%40mail.yahoo.com.

Wesley Peng

unread,
Jun 4, 2020, 3:16:03 AM6/4/20
to Dimas Prawira, golan...@googlegroups.com
Thanks. how about the sample of general socket listener with SSL rather
than net/http implementation?

Regards

Dimas Prawira wrote:
> Here is an example running server with TLS
>
> package main
>
> import (
> "net/http"
> "log"
> )
>
> func HelloServer(w http.ResponseWriter,req *http.Request) {
> w.Header().Set("Content-Type","text/plain")
> w.Write([]byte("This is an example server.\n"))
> }
>
> func main() {
> http.HandleFunc("/hello",HelloServer)
> err := http.ListenAndServeTLS(":443","server.crt","server.key",nil)
> if err != nil {
> log.Fatal("ListenAndServe: ",err)
> }
> }


--
Wesley Peng
wesle...@aol.com

Brian Candler

unread,
Jun 4, 2020, 4:48:32 AM6/4/20
to golang-nuts
https://golang.org/pkg/crypto/tls/#example_LoadX509KeyPair
sets up a basic listener.  Once you have this, it's just like a normal socket accept. See
for how to accept connections and handle them in their own goroutines.

Manlio Perillo

unread,
Jun 4, 2020, 4:22:37 PM6/4/20
to golang-nuts
See https://golang.org/pkg/crypto/tls/#Conn about how to make a secure network connection.  There is an example for the Dial function.
For the normal network API see https://golang.org/pkg/net/

Manlio

Reply all
Reply to author
Forward
0 new messages