Best Gauge code snippet using conn.SetDeadline
socks5.go
Source:socks5.go
...102 buf = append(buf, byte(len(host)))103 buf = append(buf, host...)104 }105 buf = append(buf, byte(port>>8), byte(port))106 (*s.conn).SetDeadline(time.Now().Add(s.timeout))107 if _, err := (*s.conn).Write(buf); err != nil {108 return errors.New("proxy: failed to write connect request to SOCKS5 proxy at " + s.addr + ": " + err.Error())109 }110 (*s.conn).SetDeadline(time.Time{})111 (*s.conn).SetDeadline(time.Now().Add(s.timeout))112 if _, err := io.ReadFull((*s.conn), buf[:4]); err != nil {113 return errors.New("proxy: failed to read connect reply from SOCKS5 proxy at " + s.addr + ": " + err.Error())114 }115 (*s.conn).SetDeadline(time.Time{})116 failure := "unknown error"117 if int(buf[1]) < len(socks5c.Socks5Errors) {118 failure = socks5c.Socks5Errors[buf[1]]119 }120 if len(failure) > 0 {121 return errors.New("proxy: SOCKS5 proxy at " + s.addr + " failed to connect: " + failure)122 }123 bytesToDiscard := 0124 switch buf[3] {125 case socks5c.ATYP_IPV4:126 bytesToDiscard = net.IPv4len127 case socks5c.ATYP_IPV6:128 bytesToDiscard = net.IPv6len129 case socks5c.ATYP_DOMAIN:130 (*s.conn).SetDeadline(time.Now().Add(s.timeout))131 _, err := io.ReadFull((*s.conn), buf[:1])132 (*s.conn).SetDeadline(time.Time{})133 if err != nil {134 return errors.New("proxy: failed to read domain length from SOCKS5 proxy at " + s.addr + ": " + err.Error())135 }136 bytesToDiscard = int(buf[0])137 default:138 return errors.New("proxy: got unknown address type " + strconv.Itoa(int(buf[3])) + " from SOCKS5 proxy at " + s.addr)139 }140 if cap(buf) < bytesToDiscard {141 buf = make([]byte, bytesToDiscard)142 } else {143 buf = buf[:bytesToDiscard]144 }145 (*s.conn).SetDeadline(time.Now().Add(s.timeout))146 if _, err := io.ReadFull((*s.conn), buf); err != nil {147 return errors.New("proxy: failed to read address from SOCKS5 proxy at " + s.addr + ": " + err.Error())148 }149 (*s.conn).SetDeadline(time.Time{})150 var ip net.IP151 ip = buf152 ipStr := ""153 if bytesToDiscard == net.IPv4len || bytesToDiscard == net.IPv6len {154 if ipv4 := ip.To4(); ipv4 != nil {155 ipStr = ipv4.String()156 } else {157 ipStr = ip.To16().String()158 }159 }160 //log.Printf("%v", ipStr)161 // Also need to discard the port number162 (*s.conn).SetDeadline(time.Now().Add(s.timeout))163 if _, err := io.ReadFull((*s.conn), buf[:2]); err != nil {164 return errors.New("proxy: failed to read port from SOCKS5 proxy at " + s.addr + ": " + err.Error())165 }166 p := binary.BigEndian.Uint16([]byte{buf[0], buf[1]})167 //log.Printf("%v", p)168 s.udpAddr = net.JoinHostPort(ipStr, fmt.Sprintf("%d", p))169 //log.Printf("%v", s.udpAddr)170 (*s.conn).SetDeadline(time.Time{})171 return nil172}173func (s *ClientConn) SendUDP(data []byte, addr string) (respData []byte, err error) {174 c, err := net.DialTimeout("udp", s.udpAddr, s.timeout)175 if err != nil {176 return177 }178 conn := c.(*net.UDPConn)179 p := socks5c.NewPacketUDP()180 p.Build(addr, data)181 conn.SetDeadline(time.Now().Add(s.timeout))182 conn.Write(p.Bytes())183 conn.SetDeadline(time.Time{})184 buf := make([]byte, 1024)185 conn.SetDeadline(time.Now().Add(s.timeout))186 n, _, err := conn.ReadFrom(buf)187 conn.SetDeadline(time.Time{})188 if err != nil {189 return190 }191 respData = buf[:n]192 return193}194func (s *ClientConn) auth(host string) error {195 // the size here is just an estimate196 buf := make([]byte, 0, 6+len(host))197 buf = append(buf, socks5c.VERSION_V5)198 if len(s.user) > 0 && len(s.user) < 256 && len(s.password) < 256 {199 buf = append(buf, 2 /* num auth methods */, socks5c.Method_NO_AUTH, socks5c.Method_USER_PASS)200 } else {201 buf = append(buf, 1 /* num auth methods */, socks5c.Method_NO_AUTH)202 }203 (*s.conn).SetDeadline(time.Now().Add(s.timeout))204 if _, err := (*s.conn).Write(buf); err != nil {205 return errors.New("proxy: failed to write greeting to SOCKS5 proxy at " + s.addr + ": " + err.Error())206 }207 (*s.conn).SetDeadline(time.Time{})208 (*s.conn).SetDeadline(time.Now().Add(s.timeout))209 if _, err := io.ReadFull((*s.conn), buf[:2]); err != nil {210 return errors.New("proxy: failed to read greeting from SOCKS5 proxy at " + s.addr + ": " + err.Error())211 }212 (*s.conn).SetDeadline(time.Time{})213 if buf[0] != 5 {214 return errors.New("proxy: SOCKS5 proxy at " + s.addr + " has unexpected version " + strconv.Itoa(int(buf[0])))215 }216 if buf[1] == 0xff {217 return errors.New("proxy: SOCKS5 proxy at " + s.addr + " requires authentication")218 }219 // See RFC 1929220 if buf[1] == socks5c.Method_USER_PASS {221 buf = buf[:0]222 buf = append(buf, 1 /* password protocol version */)223 buf = append(buf, uint8(len(s.user)))224 buf = append(buf, s.user...)225 buf = append(buf, uint8(len(s.password)))226 buf = append(buf, s.password...)227 (*s.conn).SetDeadline(time.Now().Add(s.timeout))228 if _, err := (*s.conn).Write(buf); err != nil {229 return errors.New("proxy: failed to write authentication request to SOCKS5 proxy at " + s.addr + ": " + err.Error())230 }231 (*s.conn).SetDeadline(time.Time{})232 (*s.conn).SetDeadline(time.Now().Add(s.timeout))233 if _, err := io.ReadFull((*s.conn), buf[:2]); err != nil {234 return errors.New("proxy: failed to read authentication reply from SOCKS5 proxy at " + s.addr + ": " + err.Error())235 }236 (*s.conn).SetDeadline(time.Time{})237 if buf[1] != 0 {238 return errors.New("proxy: SOCKS5 proxy at " + s.addr + " rejected username/password")239 }240 }241 return nil242}...
client.go
Source:client.go
...95 buf = append(buf, byte(len(host)))96 buf = append(buf, host...)97 }98 buf = append(buf, byte(port>>8), byte(port))99 (*s.conn).SetDeadline(time.Now().Add(s.timeout))100 if _, err := (*s.conn).Write(buf); err != nil {101 return errors.New("proxy: failed to write connect request to SOCKS5 proxy at " + s.addr + ": " + err.Error())102 }103 (*s.conn).SetDeadline(time.Time{})104 (*s.conn).SetDeadline(time.Now().Add(s.timeout))105 if _, err := io.ReadFull((*s.conn), buf[:4]); err != nil {106 return errors.New("proxy: failed to read connect reply from SOCKS5 proxy at " + s.addr + ": " + err.Error())107 }108 (*s.conn).SetDeadline(time.Time{})109 failure := "unknown error"110 if int(buf[1]) < len(socks5Errors) {111 failure = socks5Errors[buf[1]]112 }113 if len(failure) > 0 {114 return errors.New("proxy: SOCKS5 proxy at " + s.addr + " failed to connect: " + failure)115 }116 bytesToDiscard := 0117 switch buf[3] {118 case ATYP_IPV4:119 bytesToDiscard = net.IPv4len120 case ATYP_IPV6:121 bytesToDiscard = net.IPv6len122 case ATYP_DOMAIN:123 (*s.conn).SetDeadline(time.Now().Add(s.timeout))124 _, err := io.ReadFull((*s.conn), buf[:1])125 (*s.conn).SetDeadline(time.Time{})126 if err != nil {127 return errors.New("proxy: failed to read domain length from SOCKS5 proxy at " + s.addr + ": " + err.Error())128 }129 bytesToDiscard = int(buf[0])130 default:131 return errors.New("proxy: got unknown address type " + strconv.Itoa(int(buf[3])) + " from SOCKS5 proxy at " + s.addr)132 }133 if cap(buf) < bytesToDiscard {134 buf = make([]byte, bytesToDiscard)135 } else {136 buf = buf[:bytesToDiscard]137 }138 (*s.conn).SetDeadline(time.Now().Add(s.timeout))139 if _, err := io.ReadFull((*s.conn), buf); err != nil {140 return errors.New("proxy: failed to read address from SOCKS5 proxy at " + s.addr + ": " + err.Error())141 }142 (*s.conn).SetDeadline(time.Time{})143 var ip net.IP144 ip = buf145 ipStr := ""146 if bytesToDiscard == net.IPv4len || bytesToDiscard == net.IPv6len {147 if ipv4 := ip.To4(); ipv4 != nil {148 ipStr = ipv4.String()149 } else {150 ipStr = ip.To16().String()151 }152 }153 //log.Printf("%v", ipStr)154 // Also need to discard the port number155 (*s.conn).SetDeadline(time.Now().Add(s.timeout))156 if _, err := io.ReadFull((*s.conn), buf[:2]); err != nil {157 return errors.New("proxy: failed to read port from SOCKS5 proxy at " + s.addr + ": " + err.Error())158 }159 p := binary.BigEndian.Uint16([]byte{buf[0], buf[1]})160 //log.Printf("%v", p)161 s.UDPAddr = net.JoinHostPort(ipStr, fmt.Sprintf("%d", p))162 //log.Printf("%v", s.udpAddr)163 (*s.conn).SetDeadline(time.Time{})164 return nil165}166func (s *ClientConn) SendUDP(data []byte, addr string) (respData []byte, err error) {167 c, err := net.DialTimeout("udp", s.UDPAddr, s.timeout)168 if err != nil {169 return170 }171 conn := c.(*net.UDPConn)172 p := NewPacketUDP()173 p.Build(addr, data)174 conn.SetDeadline(time.Now().Add(s.timeout))175 conn.Write(p.Bytes())176 conn.SetDeadline(time.Time{})177 buf := make([]byte, 1024)178 conn.SetDeadline(time.Now().Add(s.timeout))179 n, _, err := conn.ReadFrom(buf)180 conn.SetDeadline(time.Time{})181 if err != nil {182 return183 }184 respData = buf[:n]185 return186}187func (s *ClientConn) handshake(host string) error {188 // the size here is just an estimate189 buf := make([]byte, 0, 6+len(host))190 buf = append(buf, VERSION_V5)191 if len(s.user) > 0 && len(s.user) < 256 && len(s.password) < 256 {192 buf = append(buf, 2 /* num auth methods */, Method_NO_AUTH, Method_USER_PASS)193 } else {194 buf = append(buf, 1 /* num auth methods */, Method_NO_AUTH)195 }196 (*s.conn).SetDeadline(time.Now().Add(s.timeout))197 if _, err := (*s.conn).Write(buf); err != nil {198 return errors.New("proxy: failed to write greeting to SOCKS5 proxy at " + s.addr + ": " + err.Error())199 }200 (*s.conn).SetDeadline(time.Time{})201 (*s.conn).SetDeadline(time.Now().Add(s.timeout))202 if _, err := io.ReadFull((*s.conn), buf[:2]); err != nil {203 return errors.New("proxy: failed to read greeting from SOCKS5 proxy at " + s.addr + ": " + err.Error())204 }205 (*s.conn).SetDeadline(time.Time{})206 if buf[0] != 5 {207 return errors.New("proxy: SOCKS5 proxy at " + s.addr + " has unexpected version " + strconv.Itoa(int(buf[0])))208 }209 if buf[1] == 0xff {210 return errors.New("proxy: SOCKS5 proxy at " + s.addr + " requires authentication")211 }212 // See RFC 1929213 if buf[1] == Method_USER_PASS {214 buf = buf[:0]215 buf = append(buf, 1 /* password protocol version */)216 buf = append(buf, uint8(len(s.user)))217 buf = append(buf, s.user...)218 buf = append(buf, uint8(len(s.password)))219 buf = append(buf, s.password...)220 (*s.conn).SetDeadline(time.Now().Add(s.timeout))221 if _, err := (*s.conn).Write(buf); err != nil {222 return errors.New("proxy: failed to write authentication request to SOCKS5 proxy at " + s.addr + ": " + err.Error())223 }224 (*s.conn).SetDeadline(time.Time{})225 (*s.conn).SetDeadline(time.Now().Add(s.timeout))226 if _, err := io.ReadFull((*s.conn), buf[:2]); err != nil {227 return errors.New("proxy: failed to read authentication reply from SOCKS5 proxy at " + s.addr + ": " + err.Error())228 }229 (*s.conn).SetDeadline(time.Time{})230 if buf[1] != 0 {231 return errors.New("proxy: SOCKS5 proxy at " + s.addr + " rejected username/password")232 }233 }234 return nil235}...
handshake.go
Source:handshake.go
...93 C2 := C0C1C2[1536+1:]94 S0S1S2 := random[1536*2+1:]95 C0[0] = 396 // > C0C197 conn.Conn.SetDeadline(time.Now().Add(timeout))98 if _, err = conn.rw.Write(C0C1); err != nil {99 return100 }101 conn.Conn.SetDeadline(time.Now().Add(timeout))102 if err = conn.rw.Flush(); err != nil {103 return104 }105 // < S0S1S2106 conn.Conn.SetDeadline(time.Now().Add(timeout))107 if _, err = io.ReadFull(conn.rw, S0S1S2); err != nil {108 return109 }110 S1 := S0S1S2[1 : 1536+1]111 if ver := pio.U32BE(S1[4:8]); ver != 0 {112 C2 = S1113 } else {114 C2 = S1115 }116 // > C2117 conn.Conn.SetDeadline(time.Now().Add(timeout))118 if _, err = conn.rw.Write(C2); err != nil {119 return120 }121 conn.Conn.SetDeadline(time.Time{})122 return123}124func (conn *Conn) HandshakeServer() (err error) {125 var random [(1 + 1536*2) * 2]byte126 C0C1C2 := random[:1536*2+1]127 C0 := C0C1C2[:1]128 C1 := C0C1C2[1 : 1536+1]129 C0C1 := C0C1C2[:1536+1]130 C2 := C0C1C2[1536+1:]131 S0S1S2 := random[1536*2+1:]132 S0 := S0S1S2[:1]133 S1 := S0S1S2[1 : 1536+1]134 S0S1 := S0S1S2[:1536+1]135 S2 := S0S1S2[1536+1:]136 // < C0C1137 conn.Conn.SetDeadline(time.Now().Add(timeout))138 if _, err = io.ReadFull(conn.rw, C0C1); err != nil {139 return140 }141 conn.Conn.SetDeadline(time.Now().Add(timeout))142 if C0[0] != 3 {143 err = fmt.Errorf("rtmp: handshake version=%d invalid", C0[0])144 return145 }146 S0[0] = 3147 clitime := pio.U32BE(C1[0:4])148 srvtime := clitime149 srvver := uint32(0x0d0e0a0d)150 cliver := pio.U32BE(C1[4:8])151 if cliver != 0 {152 var ok bool153 var digest []byte154 if ok, digest = hsParse1(C1, hsClientPartialKey, hsServerFullKey); !ok {155 err = fmt.Errorf("rtmp: handshake server: C1 invalid")156 return157 }158 hsCreate01(S0S1, srvtime, srvver, hsServerPartialKey)159 hsCreate2(S2, digest)160 } else {161 copy(S1, C2)162 copy(S2, C1)163 }164 // > S0S1S2165 conn.Conn.SetDeadline(time.Now().Add(timeout))166 if _, err = conn.rw.Write(S0S1S2); err != nil {167 return168 }169 conn.Conn.SetDeadline(time.Now().Add(timeout))170 if err = conn.rw.Flush(); err != nil {171 return172 }173 // < C2174 conn.Conn.SetDeadline(time.Now().Add(timeout))175 if _, err = io.ReadFull(conn.rw, C2); err != nil {176 return177 }178 conn.Conn.SetDeadline(time.Time{})179 return180}...
SetDeadline
Using AI Code Generation
1import (2func main() {3 conn, err := net.Dial("tcp", "www.google.com:80")4 if err != nil {5 fmt.Println(err)6 }7 conn.SetDeadline(time.Now().Add(1 * time.Second))8 fmt.Println("Connected to server")9 defer conn.Close()10}
SetDeadline
Using AI Code Generation
1import (2func main() {3 conn, err := net.Dial("tcp", "localhost:8080")4 if err != nil {5 fmt.Println(err)6 }7 defer conn.Close()8 conn.SetDeadline(time.Now().Add(10 * time.Second))9 for {10 n, err := conn.Read(buf[0:])11 if err != nil {12 fmt.Println(err)13 }14 fmt.Println(string(buf[0:n]))15 }16}
SetDeadline
Using AI Code Generation
1import (2func main() {3 fmt.Println("Starting the server ...")4 ln, err := net.Listen("tcp", ":8081")5 if err != nil {6 fmt.Println(err)7 }8 for {9 conn, err := ln.Accept()10 if err != nil {11 fmt.Println(err)12 }13 conn.SetDeadline(time.Now().Add(5 * time.Second))14 go handleRequest(conn)15 }16}17func handleRequest(conn net.Conn) {18 buf := make([]byte, 1024)19 reqLen, err := conn.Read(buf)20 if err != nil {21 fmt.Println("Error reading:", err.Error())22 }23 if reqLen > 0 {24 fmt.Printf("Received data: %v25", string(buf))26 }27 conn.Close()28}
SetDeadline
Using AI Code Generation
1import (2func main() {3 conn, err := net.DialTimeout("tcp", "golang.org:80", 5 * time.Second)4 if err != nil {5 fmt.Println(err)6 }7 defer conn.Close()8 conn.SetDeadline(time.Now().Add(10 * time.Second))9 conn.SetReadDeadline(time.Now().Add(5 * time.Second))10 conn.SetWriteDeadline(time.Now().Add(5 * time.Second))11}12The SetDeadline method of the net.Listener class takes a time.Time type value as an argument. The time.Time type value can be created by using the time.Now() function. The time.Now() function returns the current time. The time.Now().Add() function adds the specified duration to the current time and returns the time.Time type value. The time.Duration type value can be created by using the
SetDeadline
Using AI Code Generation
1func main() {2 ln, err := net.Listen("tcp", ":8080")3 if err != nil {4 }5 for {6 conn, err := ln.Accept()7 if err != nil {8 }9 conn.SetDeadline(time.Now().Add(2 * time.Second))10 reader := bufio.NewReader(conn)11 str, err := reader.ReadString('12 if err != nil {13 }14 fmt.Print("Message Received:", string(str))15 newmessage := strings.ToUpper(str)16 conn.Write([]byte(newmessage + "17 }18}19I hope this article helped you to understand how to use SetDeadline() method of conn class in Golang. If you have any doubts, please leave a comment below. Thanks for reading!20Recommended Posts: Golang | net.Conn.Write() Method21Golang | net.Conn.Read() Method22Golang | net.Conn.LocalAddr() Method23Golang | net.Conn.RemoteAddr() Method24Golang | net.Conn.SetDeadline() Method25Golang | net.Conn.SetReadDeadline() Method26Golang | net.Conn.SetWriteDeadline() Method27Golang | net.Dial() Method28Golang | net.DialTimeout() Method29Golang | net.DialUDP() Method30Golang | net.DialTCP() Method31Golang | net.DialUnix() Method32Golang | net.LookupAddr() Method33Golang | net.LookupCNAME() Method34Golang | net.LookupHost() Method35Golang | net.LookupIP() Method36Golang | net.LookupMX() Method37Golang | net.LookupNS() Method38Golang | net.LookupPort() Method39Golang | net.LookupSRV() Method40Golang | net.LookupTXT() Method41Golang | net.ParseCIDR() Method42Golang | net.ParseIP() Method43Golang | net.ParseMAC() Method44Golang | net.ParseUnixAddr() Method45Golang | net.Pipe() Method
SetDeadline
Using AI Code Generation
1import (2func main() {3 conn, err := net.Dial("tcp", "localhost:6000")4 if err != nil {5 fmt.Println(err)6 }7 defer conn.Close()8 conn.SetDeadline(time.Now().Add(2 * time.Second))9 req := make([]byte, 4096)10 _, err = conn.Read(req)11 if err != nil {12 fmt.Println("Error reading:", err.Error())13 }14}15import (16func main() {17 conn, err := net.Dial("tcp", "localhost:6000")18 if err != nil {19 fmt.Println(err)20 }21 defer conn.Close()22 conn.SetReadDeadline(time.Now().Add(2 * time.Second))23 req := make([]byte, 4096)24 _, err = conn.Read(req)25 if err != nil {26 fmt.Println("Error reading:", err.Error())27 }28}29import (30func main() {31 conn, err := net.Dial("tcp", "localhost:6000")32 if err != nil {33 fmt.Println(err)34 }35 defer conn.Close()
Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.
You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.
Get 100 minutes of automation test minutes FREE!!