Created
August 9, 2011 20:27
-
-
Save zhujo01/1135104 to your computer and use it in GitHub Desktop.
GO TLS client code issue
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( "fmt" ; "crypto/tls"; "os" ) | |
func main() { | |
conn , err := tls.Dial("tcp", "127.0.0.1:8000", nil) | |
if err != nil { | |
fmt.Println("Fatal error ", err.String()) | |
os.Exit(1) | |
} | |
defer conn.Close() | |
state := conn.ConnectionState() | |
fmt.Println("handshaked: ", state.HandshakeComplete) | |
fmt.Println("NegotiatedProtocolIsMutual: ", state.NegotiatedProtocolIsMutual) | |
fmt.Println("local address: ", conn.LocalAddr()) | |
conn.Write([]byte("Hello")) | |
var retbuf []byte | |
n, error := conn.Read(retbuf) | |
fmt.Println(n, " bytes read from socket", error) | |
fmt.Println(string(retbuf)) | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
server output: | |
root@ip-10-77-38-236:/home/ubuntu/go/jon/tls# ./server | |
Listening | |
Accepted | |
Trying to read | |
Hello | |
Echo done | |
Trying to read | |
client output: | |
root@ip-10-77-38-236:/home/ubuntu/# ./client | |
handshaked: true | |
NegotiatedProtocolIsMutual: true | |
local address: 127.0.0.1:53980 | |
0 bytes read from socket <nil> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ("fmt"; "os"; "crypto/tls"; "net"; "crypto/rand"; "time") | |
func main() { | |
cert, err := tls.LoadX509KeyPair("/etc/ssl/certs/ssl-cert-snakeoil.pem", "/etc/ssl/private/ssl-cert-snakeoil.key") | |
//cert, err := tls.LoadX509KeyPair("jan.newmarch.name.pem", "private.pem") | |
checkError(err) | |
config := tls.Config {Certificates: []tls.Certificate {cert}} | |
now := time.Seconds() | |
config.Time = func() int64 { return now } | |
config.Rand = rand.Reader | |
service := "0.0.0.0:8000" | |
listener, err := tls.Listen("tcp", service, &config) | |
checkError(err) | |
fmt.Println("Listening") | |
for { | |
conn, err := listener.Accept() | |
if err != nil { | |
fmt.Println(err.String()) | |
continue | |
} | |
fmt.Println("Accepted") | |
//tlsConn := tls.Server(conn, &config) | |
go handleClient(conn) | |
} | |
} | |
func handleClient(conn net.Conn) { | |
defer conn.Close() | |
var buf [512]byte | |
for { | |
fmt.Println("Trying to read") | |
n, err := conn.Read(buf[0:]) | |
if err != nil { | |
return | |
} | |
fmt.Println(string(buf[0:100])) | |
_, err2 := conn.Write(buf[0:n]) | |
if err2 != nil { | |
return | |
} | |
fmt.Println("Echo done") | |
} | |
} | |
func checkError(err os.Error) { | |
if err != nil { | |
fmt.Println("Fatal error ", err.String()) | |
os.Exit(1) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment