Skip to content

Commit

Permalink
update example for tls
Browse files Browse the repository at this point in the history
  • Loading branch information
liujianping committed Mar 19, 2020
1 parent d7f84d0 commit 168dadd
Show file tree
Hide file tree
Showing 5 changed files with 53 additions and 0 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
example/out/
44 changes: 44 additions & 0 deletions example/client/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
package main

import (
"crypto/tls"
"crypto/x509"
"fmt"
"io"
"log"
)

func main() {
config := tls.Config{Certificates: []tls.Certificate{}, InsecureSkipVerify: true}
conn, err := tls.Dial("tcp", "127.0.0.1:8080", &config)
if err != nil {
log.Fatalf("client: dial: %s", err)
}
defer conn.Close()
log.Println("client: connected to: ", conn.RemoteAddr())

state := conn.ConnectionState()
for _, v := range state.PeerCertificates {
fmt.Println(x509.MarshalPKIXPublicKey(v.PublicKey))
fmt.Println(v.Subject)
}
log.Println("client: handshake: ", state.HandshakeComplete)
log.Println("client: mutual: ", state.NegotiatedProtocolIsMutual)

reply := make([]byte, 256)
n, err := conn.Read(reply)
log.Printf("client: read %q (%d bytes)", string(reply[:n]), n)

message := "Hello\n"
n, err = io.WriteString(conn, message)
if err != nil {
log.Fatalf("client: write: %s", err)
}
log.Printf("client: wrote %q (%d bytes)", message, n)

//reply := make([]byte, 256)
n, err = conn.Read(reply)
log.Printf("client: read %q (%d bytes)", string(reply[:n]), n)
log.Print("client: exiting")

}
5 changes: 5 additions & 0 deletions example/server.go → example/server/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
"github.com/x-mod/glog"
"github.com/x-mod/routine"
"github.com/x-mod/tcpserver"
"github.com/x-mod/tlsconfig"
"golang.org/x/net/trace"
)

Expand All @@ -24,6 +25,9 @@ func main() {
tcpserver.Address("127.0.0.1:8080"),
tcpserver.TCPHandler(EchoHandler),
tcpserver.NetTrace(true),
tcpserver.TLSConfig(tlsconfig.New(
tlsconfig.CertKeyPair("out/server.crt", "out/server.key"),
)),
)
log.Println("tcpserver serving ...")
if err := routine.Main(
Expand All @@ -39,6 +43,7 @@ func EchoHandler(ctx context.Context, con net.Conn) error {
defer con.Close()
c := textproto.NewConn(con)

c.PrintfLine("HELLO From Server\r\n")
for {
select {
case <-ctx.Done():
Expand Down
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,6 @@ require (
github.com/x-mod/event v0.0.1
github.com/x-mod/glog v0.1.0
github.com/x-mod/routine v1.3.0
github.com/x-mod/tlsconfig v0.0.2
golang.org/x/net v0.0.0-20180826012351-8a410e7b638d
)
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ github.com/x-mod/routine v1.3.0 h1:WvQZAYBk8ZuYQpxdU7+S6U5zPdbbKo8Q5PVPL5iY7Sk=
github.com/x-mod/routine v1.3.0/go.mod h1:sahp55IQiyf6cJar5lCXcH9pwE1Cd/cuBwTbglCyX/I=
github.com/x-mod/sigtrap v0.1.0 h1:tCFFx0ZluBtzkcWwJWxxaXK1oVek5O4LJfB9iTHITb4=
github.com/x-mod/sigtrap v0.1.0/go.mod h1:cGdazs+3wsvER2vgbznHIyzXhefdjs2Q5lCR9ETBVA0=
github.com/x-mod/tlsconfig v0.0.2 h1:W6qAvuaMfzSf78rpahel01PlzI/HylZCM8mq2BUz3BE=
github.com/x-mod/tlsconfig v0.0.2/go.mod h1:yiTPHfiJzNrZPOaXweRiTt7F4LWGVAqTVLP1N7UMy9g=
golang.org/x/lint v0.0.0-20181026193005-c67002cb31c3/go.mod h1:UVdnD1Gm6xHRNCYTkRU2/jEulfH38KcIWyp/GAMgvoE=
golang.org/x/net v0.0.0-20180826012351-8a410e7b638d h1:g9qWBGx4puODJTMVyoPrpoxPFgVGd+z1DZwjfRu4d0I=
golang.org/x/net v0.0.0-20180826012351-8a410e7b638d/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4=
Expand Down

0 comments on commit 168dadd

Please sign in to comment.