2018-07-11 23:27:47 +08:00
|
|
|
package util
|
2017-12-16 23:59:46 +08:00
|
|
|
|
|
|
|
import (
|
2017-12-18 19:35:09 +08:00
|
|
|
"encoding/base64"
|
2018-01-18 14:53:44 +08:00
|
|
|
"encoding/json"
|
|
|
|
"errors"
|
2017-12-16 23:59:46 +08:00
|
|
|
"fmt"
|
2018-07-12 15:23:34 +08:00
|
|
|
"io"
|
2017-12-18 19:35:09 +08:00
|
|
|
"io/ioutil"
|
2017-12-16 23:59:46 +08:00
|
|
|
"net"
|
2017-12-18 19:35:09 +08:00
|
|
|
"net/http"
|
2018-01-23 17:11:59 +08:00
|
|
|
"net/url"
|
2018-01-18 14:53:44 +08:00
|
|
|
"strings"
|
2017-12-16 23:59:46 +08:00
|
|
|
"time"
|
|
|
|
|
2018-01-18 14:53:44 +08:00
|
|
|
"github.com/fatedier/frp/client"
|
2017-12-16 23:59:46 +08:00
|
|
|
frpNet "github.com/fatedier/frp/utils/net"
|
|
|
|
)
|
|
|
|
|
2018-07-11 23:27:47 +08:00
|
|
|
func GetProxyStatus(statusAddr string, user string, passwd string, name string) (status *client.ProxyStatusResp, err error) {
|
|
|
|
req, err := http.NewRequest("GET", "http://"+statusAddr+"/api/status", nil)
|
2018-01-18 14:53:44 +08:00
|
|
|
if err != nil {
|
|
|
|
return status, err
|
|
|
|
}
|
|
|
|
|
2018-07-11 23:27:47 +08:00
|
|
|
authStr := "Basic " + base64.StdEncoding.EncodeToString([]byte(user+":"+passwd))
|
2018-01-18 14:53:44 +08:00
|
|
|
req.Header.Add("Authorization", authStr)
|
|
|
|
resp, err := http.DefaultClient.Do(req)
|
|
|
|
if err != nil {
|
|
|
|
return status, err
|
2019-05-30 12:11:59 +08:00
|
|
|
}
|
2019-05-31 15:56:05 +08:00
|
|
|
defer resp.Body.Close()
|
2019-05-30 12:11:59 +08:00
|
|
|
if resp.StatusCode != 200 {
|
|
|
|
return status, fmt.Errorf("admin api status code [%d]", resp.StatusCode)
|
|
|
|
}
|
|
|
|
body, err := ioutil.ReadAll(resp.Body)
|
|
|
|
if err != nil {
|
|
|
|
return status, err
|
|
|
|
}
|
|
|
|
allStatus := &client.StatusResp{}
|
|
|
|
err = json.Unmarshal(body, &allStatus)
|
|
|
|
if err != nil {
|
|
|
|
return status, fmt.Errorf("unmarshal http response error: %s", strings.TrimSpace(string(body)))
|
|
|
|
}
|
|
|
|
for _, s := range allStatus.Tcp {
|
|
|
|
if s.Name == name {
|
|
|
|
return &s, nil
|
2018-01-18 14:53:44 +08:00
|
|
|
}
|
2019-05-30 12:11:59 +08:00
|
|
|
}
|
|
|
|
for _, s := range allStatus.Udp {
|
|
|
|
if s.Name == name {
|
|
|
|
return &s, nil
|
2018-01-18 14:53:44 +08:00
|
|
|
}
|
2019-05-30 12:11:59 +08:00
|
|
|
}
|
|
|
|
for _, s := range allStatus.Http {
|
|
|
|
if s.Name == name {
|
|
|
|
return &s, nil
|
2018-01-18 14:53:44 +08:00
|
|
|
}
|
2019-05-30 12:11:59 +08:00
|
|
|
}
|
|
|
|
for _, s := range allStatus.Https {
|
|
|
|
if s.Name == name {
|
|
|
|
return &s, nil
|
2018-01-18 14:53:44 +08:00
|
|
|
}
|
2019-05-30 12:11:59 +08:00
|
|
|
}
|
|
|
|
for _, s := range allStatus.Stcp {
|
|
|
|
if s.Name == name {
|
|
|
|
return &s, nil
|
2018-01-18 14:53:44 +08:00
|
|
|
}
|
2019-05-30 12:11:59 +08:00
|
|
|
}
|
|
|
|
for _, s := range allStatus.Xtcp {
|
|
|
|
if s.Name == name {
|
|
|
|
return &s, nil
|
2018-01-18 14:53:44 +08:00
|
|
|
}
|
|
|
|
}
|
2019-05-30 12:11:59 +08:00
|
|
|
|
2018-01-18 14:53:44 +08:00
|
|
|
return status, errors.New("no proxy status found")
|
|
|
|
}
|
|
|
|
|
2018-07-12 15:23:34 +08:00
|
|
|
func ReloadConf(reloadAddr string, user string, passwd string) error {
|
|
|
|
req, err := http.NewRequest("GET", "http://"+reloadAddr+"/api/reload", nil)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
authStr := "Basic " + base64.StdEncoding.EncodeToString([]byte(user+":"+passwd))
|
|
|
|
req.Header.Add("Authorization", authStr)
|
|
|
|
resp, err := http.DefaultClient.Do(req)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2019-05-30 12:11:59 +08:00
|
|
|
defer resp.Body.Close()
|
|
|
|
|
|
|
|
if resp.StatusCode != 200 {
|
|
|
|
return fmt.Errorf("admin api status code [%d]", resp.StatusCode)
|
|
|
|
}
|
|
|
|
io.Copy(ioutil.Discard, resp.Body)
|
2018-07-12 15:23:34 +08:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2018-07-11 23:27:47 +08:00
|
|
|
func SendTcpMsg(addr string, msg string) (res string, err error) {
|
2017-12-16 23:59:46 +08:00
|
|
|
c, err := frpNet.ConnectTcpServer(addr)
|
|
|
|
if err != nil {
|
|
|
|
err = fmt.Errorf("connect to tcp server error: %v", err)
|
|
|
|
return
|
|
|
|
}
|
2017-12-18 19:35:09 +08:00
|
|
|
defer c.Close()
|
2018-07-11 23:27:47 +08:00
|
|
|
return SendTcpMsgByConn(c, msg)
|
2018-01-23 17:11:59 +08:00
|
|
|
}
|
2017-12-16 23:59:46 +08:00
|
|
|
|
2018-07-11 23:27:47 +08:00
|
|
|
func SendTcpMsgByConn(c net.Conn, msg string) (res string, err error) {
|
2017-12-16 23:59:46 +08:00
|
|
|
timer := time.Now().Add(5 * time.Second)
|
|
|
|
c.SetDeadline(timer)
|
|
|
|
c.Write([]byte(msg))
|
|
|
|
|
|
|
|
buf := make([]byte, 2048)
|
|
|
|
n, errRet := c.Read(buf)
|
|
|
|
if errRet != nil {
|
|
|
|
err = fmt.Errorf("read from tcp server error: %v", errRet)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
return string(buf[:n]), nil
|
|
|
|
}
|
|
|
|
|
2018-07-11 23:27:47 +08:00
|
|
|
func SendUdpMsg(addr string, msg string) (res string, err error) {
|
2017-12-16 23:59:46 +08:00
|
|
|
udpAddr, errRet := net.ResolveUDPAddr("udp", addr)
|
|
|
|
if errRet != nil {
|
|
|
|
err = fmt.Errorf("resolve udp addr error: %v", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
conn, errRet := net.DialUDP("udp", nil, udpAddr)
|
|
|
|
if errRet != nil {
|
|
|
|
err = fmt.Errorf("dial udp server error: %v", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
defer conn.Close()
|
|
|
|
_, err = conn.Write([]byte(msg))
|
|
|
|
if err != nil {
|
|
|
|
err = fmt.Errorf("write to udp server error: %v", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
buf := make([]byte, 2048)
|
|
|
|
n, errRet := conn.Read(buf)
|
|
|
|
if errRet != nil {
|
|
|
|
err = fmt.Errorf("read from udp server error: %v", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
return string(buf[:n]), nil
|
|
|
|
}
|
2017-12-18 19:35:09 +08:00
|
|
|
|
2018-07-11 23:27:47 +08:00
|
|
|
func SendHttpMsg(method, urlStr string, host string, headers map[string]string, proxy string) (code int, body string, header http.Header, err error) {
|
2018-01-23 17:11:59 +08:00
|
|
|
req, errRet := http.NewRequest(method, urlStr, nil)
|
2017-12-18 19:35:09 +08:00
|
|
|
if errRet != nil {
|
|
|
|
err = errRet
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if host != "" {
|
|
|
|
req.Host = host
|
|
|
|
}
|
2018-05-20 23:55:22 +08:00
|
|
|
for k, v := range headers {
|
2017-12-18 19:35:09 +08:00
|
|
|
req.Header.Set(k, v)
|
|
|
|
}
|
2018-01-23 17:11:59 +08:00
|
|
|
|
|
|
|
tr := &http.Transport{
|
|
|
|
DialContext: (&net.Dialer{
|
|
|
|
Timeout: 30 * time.Second,
|
|
|
|
KeepAlive: 30 * time.Second,
|
|
|
|
DualStack: true,
|
|
|
|
}).DialContext,
|
|
|
|
MaxIdleConns: 100,
|
|
|
|
IdleConnTimeout: 90 * time.Second,
|
|
|
|
TLSHandshakeTimeout: 10 * time.Second,
|
|
|
|
ExpectContinueTimeout: 1 * time.Second,
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(proxy) != 0 {
|
|
|
|
tr.Proxy = func(req *http.Request) (*url.URL, error) {
|
|
|
|
return url.Parse(proxy)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
client := http.Client{
|
|
|
|
Transport: tr,
|
|
|
|
}
|
|
|
|
|
|
|
|
resp, errRet := client.Do(req)
|
2017-12-18 19:35:09 +08:00
|
|
|
if errRet != nil {
|
|
|
|
err = errRet
|
|
|
|
return
|
|
|
|
}
|
|
|
|
code = resp.StatusCode
|
2018-05-20 23:55:22 +08:00
|
|
|
header = resp.Header
|
2017-12-18 19:35:09 +08:00
|
|
|
buf, errRet := ioutil.ReadAll(resp.Body)
|
|
|
|
if errRet != nil {
|
|
|
|
err = errRet
|
|
|
|
return
|
|
|
|
}
|
|
|
|
body = string(buf)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2018-07-11 23:27:47 +08:00
|
|
|
func BasicAuth(username, passwd string) string {
|
2017-12-18 19:35:09 +08:00
|
|
|
auth := username + ":" + passwd
|
|
|
|
return "Basic " + base64.StdEncoding.EncodeToString([]byte(auth))
|
|
|
|
}
|