52 lines
954 B
Go
52 lines
954 B
Go
package globals
|
|
|
|
import (
|
|
"encoding/json"
|
|
"fmt"
|
|
"net/http"
|
|
"strings"
|
|
"time"
|
|
)
|
|
|
|
const (
|
|
PermitEndpoint = "/api/permit"
|
|
)
|
|
|
|
var Proxy *ProxyClient
|
|
|
|
type ProxyClient struct {
|
|
}
|
|
|
|
func initProxy() {
|
|
Proxy = &ProxyClient{}
|
|
}
|
|
|
|
type ProxyPermitConfig struct {
|
|
Id int32 `json:"id"`
|
|
Whitelists *[]string `json:"whitelists,omitempty"`
|
|
Username *string `json:"username,omitempty"`
|
|
Password *string `json:"password,omitempty"`
|
|
Expire time.Time `json:"expire"`
|
|
}
|
|
|
|
func (p *ProxyClient) Permit(proxy string, config []*ProxyPermitConfig) error {
|
|
|
|
str, err := json.Marshal(config)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
body := strings.NewReader(string(str))
|
|
resp, err := http.Post(fmt.Sprintf("%s:8848%s", proxy, PermitEndpoint), "application/json", body)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
defer resp.Body.Close()
|
|
|
|
if resp.StatusCode != http.StatusOK {
|
|
return fmt.Errorf("配置端口许可失败: %s", resp.Status)
|
|
}
|
|
|
|
return nil
|
|
}
|