提交代理配置结果到长效网关
This commit is contained in:
51
web/globals/proxy.go
Normal file
51
web/globals/proxy.go
Normal file
@@ -0,0 +1,51 @@
|
||||
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
|
||||
}
|
||||
Reference in New Issue
Block a user