1 package main 2 3 import ( 4 "net" 5 "sync" 6 "crypto/rand" 7 "crypto/tls " 8 "log" 9 "encoding/j son" 10 ) 11 12 13 func 14 main() { 15 16 cert, err := tls.LoadX509Ke yPair( "OreOre.pem ", "OreOre.pem " ) 17 if err != nil { log.Fatalf( "LoadX509Ke yPair:%v", err ) } 18 config := &tls.Config{ 19 Certificat es: []tls.Certificat e{ cert }, 20 ClientAuth : tls.VerifyClie ntCertIfGi ven, 21 ServerName : "example.co m", 22 Rand: rand.Reader, 23 } 24 25 connection s := map[ net.Conn ] interface{} {} 26 mutex := sync.RWMutex{} 27 28 broadcaste r := make( chan interface{} ) 29 go func() { 30 for { 31 data, _ := json.Marshal( <- broadcaste r ) 32 mutex.RLock() 33 conns := connection s 34 mutex.RUnlock() 35 for conn, _ := range conns { conn.Write( data ) } 36 } 37 }() 38 39 listener, _ := tls.Listen( "tcp", ":6666", config ) 40 defer listener.Close() 41 for { 42 conn, _ := listener.Accept() 43 mutex.Lock() 44 connection s[ conn ] = "" 45 mutex.Unlock() 46 go func() { 47 decoder := json.NewDecoder ( conn ) 48 for { 49 var w interface{} 50 err := decoder.Decode( &w ) 51 if err != nil { break } 52 broadcaste r <- w 53 } 54 mutex.Lock() 55 delete( connection s, conn ) 56 mutex.Unlock() 57 conn.Close() 58 }() 59 } 60 }
証明書がオレオレなので openssl で検証。
openssl s_client -tls1 -connect localhost:
posted by
Saturn
on Wed 24 Dec 2014
at 22:09