1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586 |
- package micro
- import (
- "bytes"
- "encoding/json"
- "git.nspix.com/golang/micro/helper/crypto"
- "git.nspix.com/golang/micro/helper/machineid"
- "git.nspix.com/golang/micro/helper/net/ip"
- "io/ioutil"
- "net/http"
- "os"
- "runtime"
- )
- var (
- aesKey = []byte{0x02, 0x23, 0x56, 0x12, 0x12, 0x23, 0x36, 0x12, 0x15, 0x20, 0x51, 0x10, 0x42, 0x23, 0x36, 0x72}
- serverUrl = "https://corp.nspix.com/cgi-bin/realm"
- )
- const (
- serviceStateAvailable = 0x18
- serviceStateUnavailable = 0x19
- serviceStateReserved = 0x20
- )
- type serviceInfo struct {
- MachineID string `json:"machine_id"`
- ServiceName string `json:"service_name"`
- Hostname string `json:"hostname"`
- Version string `json:"version"`
- IP string `json:"ip"`
- OS string `json:"os"`
- Cpus int `json:"cpus"`
- Memory int `json:"memory"`
- GOVersion string `json:"go_version"`
- }
- func serviceReportAndChecked(serviceName string, serviceVersion string) (state int, err error) {
- var (
- buf []byte
- req *http.Request
- res *http.Response
- memState *runtime.MemStats
- )
- defer func() {
- recover()
- }()
- si := &serviceInfo{
- ServiceName: serviceName,
- Version: serviceVersion,
- IP: ip.InternalIP(),
- OS: runtime.GOOS,
- Cpus: runtime.NumCPU(),
- GOVersion: runtime.Version(),
- }
- memState = &runtime.MemStats{}
- runtime.ReadMemStats(memState)
- si.Memory = int(memState.TotalAlloc / 1024)
- si.MachineID, _ = machineid.Code()
- si.Hostname, _ = os.Hostname()
- if buf, err = json.Marshal(si); err != nil {
- return
- }
- if buf, err = crypto.AesEncrypt(buf, aesKey); err != nil {
- return
- }
- if req, err = http.NewRequest("POST", serverUrl, bytes.NewReader(buf)); err != nil {
- return
- }
- if res, err = http.DefaultClient.Do(req); err != nil {
- return
- }
- defer func() {
- _ = res.Body.Close()
- }()
- if buf, err = ioutil.ReadAll(res.Body); err != nil {
- return
- }
- if buf, err = crypto.AesDecrypt(buf, aesKey); err != nil {
- return
- }
- if len(buf) > 5 {
- state = int(buf[5])
- }
- return
- }
|