mirror of https://github.com/fatedier/frp
You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
41 lines
952 B
41 lines
952 B
package request
|
|
|
|
import (
|
|
"fmt"
|
|
"net"
|
|
"time"
|
|
)
|
|
|
|
func SendTCPRequest(port int, content []byte, timeout time.Duration) (string, error) {
|
|
c, err := net.Dial("tcp", fmt.Sprintf("127.0.0.1:%d", port))
|
|
if err != nil {
|
|
return "", fmt.Errorf("connect to tcp server error: %v", err)
|
|
}
|
|
defer c.Close()
|
|
|
|
c.SetDeadline(time.Now().Add(timeout))
|
|
return sendRequestByConn(c, content)
|
|
}
|
|
|
|
func SendUDPRequest(port int, content []byte, timeout time.Duration) (string, error) {
|
|
c, err := net.Dial("udp", fmt.Sprintf("127.0.0.1:%d", port))
|
|
if err != nil {
|
|
return "", fmt.Errorf("connect to udp server error: %v", err)
|
|
}
|
|
defer c.Close()
|
|
|
|
c.SetDeadline(time.Now().Add(timeout))
|
|
return sendRequestByConn(c, content)
|
|
}
|
|
|
|
func sendRequestByConn(c net.Conn, content []byte) (string, error) {
|
|
c.Write(content)
|
|
|
|
buf := make([]byte, 2048)
|
|
n, err := c.Read(buf)
|
|
if err != nil {
|
|
return "", fmt.Errorf("read error: %v", err)
|
|
}
|
|
return string(buf[:n]), nil
|
|
}
|