Advertisement
Guest User

Untitled

a guest
Jul 14th, 2016
202
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Go 2.10 KB | None | 0 0
  1. package main
  2.  
  3. import "crypto/tls"
  4. import "bufio"
  5. import "encoding/binary"
  6. import "fmt"
  7. import "bytes"
  8. import "time"
  9.  
  10.  
  11. type packet struct {
  12.    a uint32
  13.    b []byte
  14. }
  15.  
  16. func pack(d string) []byte {
  17.    
  18.    
  19.     buf := bytes.Buffer{}
  20.     u := len([]byte(d))
  21.     p := packet{
  22.         a: uint32(u),      
  23.         b: []byte(d),
  24.     }
  25.    
  26.     err := binary.Write(&buf, binary.BigEndian, p.a)
  27.    
  28.     if err != nil {
  29.        fmt.Println(err)
  30.     }
  31.    
  32.     buf.Write(p.b)
  33.        
  34.     return buf.Bytes()
  35.      
  36. }
  37.  
  38. func initIt() {
  39.    
  40. }
  41.  
  42.  
  43. func main() {
  44.    
  45.     initIt()
  46.    
  47. fmt.Println("-------> Connect")
  48.  
  49.     connect:
  50.         conf := &tls.Config{ // enable TLS client encryption
  51.             InsecureSkipVerify: true,
  52.         }
  53.        
  54.         tlsConn, err := tls.Dial("tcp","127.0.0.1:5050", conf);
  55.         if err != nil {
  56.             fmt.Println(err)
  57.             time.Sleep(5000 * time.Millisecond) // wait 5 secs
  58.             goto connect
  59.         }
  60.  
  61.         defer tlsConn.Close()
  62.    
  63.         reader := bufio.NewReader(tlsConn)
  64.         //writer := bufio.NewWriter(tlsConn) // not neccessary, just use c.Write(...)
  65.    
  66.     // client login
  67. fmt.Println("-------> Client login")
  68.    
  69.     tlsConn.Write(pack("login test:test"))  
  70.    
  71.     // //!REMOVEME d1.Reset(
  72.     time.Sleep(1000 * time.Millisecond)
  73.    
  74.     var p uint32  // packet size
  75.     binary.Read(reader, binary.BigEndian, &p)
  76.     fmt.Println(int(p))
  77.    
  78.     buf1 := make([]byte, int(p))
  79. fmt.Println("-------> Client login [read response...]")
  80.     reader.Read(buf1)
  81.     fmt.Println(string(buf1))
  82.     fmt.Println(len(string(buf1)))
  83.    
  84.    
  85.    
  86. fmt.Println("-------> Client ping")
  87.     tlsConn.Write(pack("ping 0000-00-00 00:00:00"))  
  88.    
  89. fmt.Println("-------> Client ping [read into p2...]")
  90.     var p2 uint32  // packet size
  91.     binary.Read(reader, binary.BigEndian, &p2)
  92.     fmt.Println(p2)
  93.    
  94.     buf1 = make([]byte, int(p2))
  95. fmt.Println("-------> Client ping [read response...]")
  96.     reader.Read(buf1)
  97.     fmt.Println(string(buf1))
  98.  
  99.     time.Sleep(50000 * time.Millisecond)
  100.    
  101. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement