mirror of
https://github.com/yusing/godoxy.git
synced 2026-04-23 09:18:51 +02:00
feat(agent): add agent iteration and count functions; refactor Forward method to return *http.Response
This commit is contained in:
@@ -1,6 +1,8 @@
|
|||||||
package agent
|
package agent
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"iter"
|
||||||
|
|
||||||
"github.com/yusing/go-proxy/internal/common"
|
"github.com/yusing/go-proxy/internal/common"
|
||||||
"github.com/yusing/go-proxy/internal/utils/functional"
|
"github.com/yusing/go-proxy/internal/utils/functional"
|
||||||
)
|
)
|
||||||
@@ -51,6 +53,14 @@ func ListAgents() []*AgentConfig {
|
|||||||
return agents
|
return agents
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func IterAgents() iter.Seq2[string, *AgentConfig] {
|
||||||
|
return agentPool.Range
|
||||||
|
}
|
||||||
|
|
||||||
|
func NumAgents() int {
|
||||||
|
return agentPool.Size()
|
||||||
|
}
|
||||||
|
|
||||||
func getAgentByAddr(addr string) (agent *AgentConfig, ok bool) {
|
func getAgentByAddr(addr string) (agent *AgentConfig, ok bool) {
|
||||||
agent, ok = agentPool.Load(addr)
|
agent, ok = agentPool.Load(addr)
|
||||||
return
|
return
|
||||||
|
|||||||
@@ -16,7 +16,7 @@ func (cfg *AgentConfig) Do(ctx context.Context, method, endpoint string, body io
|
|||||||
return cfg.httpClient.Do(req)
|
return cfg.httpClient.Do(req)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (cfg *AgentConfig) Forward(req *http.Request, endpoint string) ([]byte, int, error) {
|
func (cfg *AgentConfig) Forward(req *http.Request, endpoint string) (*http.Response, error) {
|
||||||
req = req.WithContext(req.Context())
|
req = req.WithContext(req.Context())
|
||||||
req.URL.Host = AgentHost
|
req.URL.Host = AgentHost
|
||||||
req.URL.Scheme = "https"
|
req.URL.Scheme = "https"
|
||||||
@@ -24,11 +24,9 @@ func (cfg *AgentConfig) Forward(req *http.Request, endpoint string) ([]byte, int
|
|||||||
req.RequestURI = ""
|
req.RequestURI = ""
|
||||||
resp, err := cfg.httpClient.Do(req)
|
resp, err := cfg.httpClient.Do(req)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, 0, err
|
return nil, err
|
||||||
}
|
}
|
||||||
defer resp.Body.Close()
|
return resp, nil
|
||||||
data, _ := io.ReadAll(resp.Body)
|
|
||||||
return data, resp.StatusCode, nil
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func (cfg *AgentConfig) Fetch(ctx context.Context, endpoint string) ([]byte, int, error) {
|
func (cfg *AgentConfig) Fetch(ctx context.Context, endpoint string) ([]byte, int, error) {
|
||||||
|
|||||||
Reference in New Issue
Block a user