(password []byte, src string, client net.Conn, timeout, udptimeout int)
| 45 | } |
| 46 | |
| 47 | func NewStreamServer(password []byte, src string, client net.Conn, timeout, udptimeout int) (Exchanger, error) { |
| 48 | if timeout != 0 { |
| 49 | if err := client.SetDeadline(time.Now().Add(time.Duration(timeout) * time.Second)); err != nil { |
| 50 | return nil, err |
| 51 | } |
| 52 | } |
| 53 | s := &StreamServer{Client: client, Timeout: timeout, src: src} |
| 54 | s.cn = x.BP12.Get().([]byte) |
| 55 | if _, err := io.ReadFull(s.Client, s.cn); err != nil { |
| 56 | x.BP12.Put(s.cn) |
| 57 | return nil, err |
| 58 | } |
| 59 | ck := x.BP32.Get().([]byte) |
| 60 | if _, err := io.ReadFull(hkdf.New(sha256.New, password, s.cn, ClientHKDFInfo), ck); err != nil { |
| 61 | x.BP12.Put(s.cn) |
| 62 | x.BP32.Put(ck) |
| 63 | return nil, err |
| 64 | } |
| 65 | cb, err := aes.NewCipher(ck) |
| 66 | if err != nil { |
| 67 | x.BP12.Put(s.cn) |
| 68 | x.BP32.Put(ck) |
| 69 | return nil, err |
| 70 | } |
| 71 | x.BP32.Put(ck) |
| 72 | s.ca, err = cipher.NewGCM(cb) |
| 73 | if err != nil { |
| 74 | x.BP12.Put(s.cn) |
| 75 | return nil, err |
| 76 | } |
| 77 | |
| 78 | s.RB = x.BP2048.Get().([]byte) |
| 79 | l, err := s.Read() |
| 80 | if err != nil { |
| 81 | x.BP12.Put(s.cn) |
| 82 | x.BP2048.Put(s.RB) |
| 83 | return nil, err |
| 84 | } |
| 85 | i := int64(binary.BigEndian.Uint32(s.RB[2+16 : 2+16+4])) |
| 86 | if time.Now().Unix()-i > 60 { |
| 87 | x.BP12.Put(s.cn) |
| 88 | x.BP2048.Put(s.RB) |
| 89 | WaitReadErr(s.Client) |
| 90 | return nil, errors.New("Expired request") |
| 91 | } |
| 92 | if i%2 == 0 { |
| 93 | s.network = "tcp" |
| 94 | } |
| 95 | if i%2 == 1 { |
| 96 | s.network = "udp" |
| 97 | s.Timeout = udptimeout |
| 98 | } |
| 99 | |
| 100 | s.sn = x.BP12.Get().([]byte) |
| 101 | if _, err := io.ReadFull(rand.Reader, s.sn); err != nil { |
| 102 | x.BP12.Put(s.cn) |
| 103 | x.BP2048.Put(s.RB) |
| 104 | x.BP12.Put(s.sn) |
no test coverage detected