init.go 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274
  1. package core
  2. import (
  3. "errors"
  4. "fmt"
  5. "io"
  6. "io/ioutil"
  7. "net"
  8. "net/http"
  9. "os"
  10. "regexp"
  11. "runtime"
  12. "strings"
  13. "time"
  14. "github.com/beego/beego/v2/adapter/httplib"
  15. "github.com/cdle/sillyplus/core/storage"
  16. "github.com/cdle/sillyplus/utils"
  17. )
  18. var version = compiled_at
  19. func GetVersion() (string, error) {
  20. v, e := httplib.Get("http://172.96.255.172:8765/api/version").String()
  21. if len(v) == 13 {
  22. if version != v && v != compiled_at {
  23. sillyGirl.Set("version", v)
  24. console.Log("发现更新,版本号", v)
  25. version = v
  26. }
  27. return v, e
  28. }
  29. return v, errors.New("版本获取失败")
  30. }
  31. func Init() {
  32. go func() {
  33. for {
  34. GetVersion()
  35. if version != compiled_at {
  36. break
  37. }
  38. time.Sleep(time.Minute * 5)
  39. }
  40. }()
  41. initLoc()
  42. sillyGirl = MakeBucket("sillyGirl")
  43. // utils.ReadYaml(utils.ExecPath+"/conf/", &Config, "https://raw.githubusercontent.com/cdle/sillyplus/main/conf/demo_config.yaml")
  44. initToHandleMessage()
  45. sillyGirl.Set("compiled_at", compiled_at)
  46. console.Log("编译版本: %s", compiled_at)
  47. initWeb()
  48. initCarry()
  49. sillyGirl.Set("started_at", time.Now().Format("2006-01-02 15:04:05"))
  50. var updates = 0
  51. storage.Watch(sillyGirl, "compiled_at", func(old, new, key string) *storage.Final {
  52. if old != new {
  53. if updates == 1 {
  54. return &storage.Final{
  55. Message: "升级中,请耐心等待...",
  56. }
  57. }
  58. updates++
  59. defer func() {
  60. updates--
  61. }()
  62. var transport *http.Transport
  63. instance, err := GetProxyTransport("https://raw.githubusercontent.com", "", nil)
  64. if err != nil {
  65. console.Error("升级代理错误:%s", err)
  66. return &storage.Final{
  67. Error: fmt.Errorf("升级代理错误::%s", err),
  68. }
  69. }
  70. if instance != nil {
  71. defer instance.Close()
  72. }
  73. if instance != nil {
  74. transport = &http.Transport{
  75. Dial: func(string, string) (net.Conn, error) {
  76. return instance, nil
  77. },
  78. MaxIdleConns: 100,
  79. IdleConnTimeout: 90 * time.Second,
  80. TLSHandshakeTimeout: 10 * time.Second,
  81. ExpectContinueTimeout: 1 * time.Second,
  82. }
  83. }
  84. var client = &http.Client{}
  85. if transport != nil {
  86. client.Transport = transport
  87. }
  88. var body io.Reader
  89. var data []byte
  90. var latest_version = ""
  91. var resp *http.Response
  92. var req *http.Request
  93. proxy := false
  94. qurl := "https://raw.githubusercontent.com/cdle/binary/main/compile_time.go"
  95. version, _ := GetVersion()
  96. if version != "" {
  97. console.Debug("正在从 github 获取版本号...")
  98. latest_version = version
  99. } else {
  100. console.Debug("正在从 cdle/binary 获取版本号...")
  101. req, _ = http.NewRequest("GET", qurl, strings.NewReader(""))
  102. resp, err = client.Do(req)
  103. if err != nil {
  104. console.Error("获取版本号错误:%s", err)
  105. // return &storage.Final{
  106. // Error: fmt.Errorf("貌似网络不太行啊:%s", err),
  107. // }
  108. goto PROXY
  109. }
  110. defer resp.Body.Close()
  111. data, _ = ioutil.ReadAll(resp.Body)
  112. latest_version = regexp.MustCompile(`\d{13}`).FindString(string(data))
  113. }
  114. if latest_version <= compiled_at {
  115. console.Debug("当前版本 %s 已是最新,无需升级", compiled_at)
  116. return &storage.Final{
  117. Message: fmt.Sprintf("当前版本 %s 已是最新,无需升级", compiled_at),
  118. }
  119. }
  120. client = &http.Client{
  121. Timeout: 30 * time.Second,
  122. }
  123. if transport != nil {
  124. client.Transport = transport
  125. }
  126. console.Debug("正在从 cdle/binary 获取最新版本 %s 编译文件...", latest_version)
  127. qurl = "https://raw.githubusercontent.com/cdle/binary/master/sillyGirl_" + runtime.GOOS + "_" + runtime.GOARCH + "_" + latest_version
  128. if runtime.GOOS == "windows" {
  129. qurl += ".exe"
  130. }
  131. req, _ = http.NewRequest("GET", qurl, strings.NewReader(""))
  132. resp, err = client.Do(req)
  133. if err != nil {
  134. console.Error("获取最新编译文件错误:%s", err)
  135. // return &storage.Final{
  136. // Error: fmt.Errorf("升级时貌似网络不太行啊:%v", err),
  137. // }
  138. goto PROXY
  139. }
  140. defer resp.Body.Close()
  141. body = resp.Body
  142. goto CREATE
  143. PROXY:
  144. //使用免费代理下载
  145. proxy = true
  146. console.Info("正在重新尝试下载...")
  147. qurl = "http://172.96.255.172:8765/api/download?version=" + compiled_at + "&goos=" + runtime.GOOS + "&goarch=" + runtime.GOARCH
  148. resp, err = http.Get(qurl)
  149. if err != nil {
  150. return &storage.Final{
  151. Error: fmt.Errorf("升级时貌似网络不太行啊"),
  152. }
  153. }
  154. defer resp.Body.Close()
  155. body = resp.Body
  156. switch resp.Header.Get("Result") {
  157. case "newest":
  158. return &storage.Final{
  159. Message: fmt.Sprintf("当前版本 %s 已是最新,无需升级", compiled_at),
  160. }
  161. case "fail":
  162. return &storage.Final{
  163. Error: fmt.Errorf("升级失败"),
  164. }
  165. case "ok":
  166. }
  167. CREATE:
  168. console.Debug("正在创建编译文件...")
  169. filename := utils.ExecPath + "/" + utils.ProcessName
  170. ready := ""
  171. if runtime.GOOS == "windows" {
  172. ready = strings.Replace(filename, ".exe", ".ready.exe", -1)
  173. } else {
  174. ready += ".ready"
  175. }
  176. f, err := os.OpenFile(ready, os.O_WRONLY|os.O_CREATE|os.O_TRUNC, 0755)
  177. if err != nil {
  178. console.Error("创建编译文件错误:%v", err)
  179. return &storage.Final{
  180. Error: fmt.Errorf("创建编译文件错误:%v", err),
  181. }
  182. }
  183. defer f.Close()
  184. i, err := io.Copy(f, body)
  185. if i < 2646140 || err != nil {
  186. if !proxy {
  187. goto PROXY
  188. }
  189. console.Error("创建编译文件错误:%v %v", i, err)
  190. return &storage.Final{
  191. Error: fmt.Errorf("创建编译文件错误:%v %v", i, err),
  192. }
  193. }
  194. if runtime.GOOS == "windows" {
  195. console.Log("正在准备重启...")
  196. go func() {
  197. time.Sleep(time.Second)
  198. utils.Daemon("ready")
  199. }()
  200. return &storage.Final{
  201. Message: "1秒重启升级!",
  202. }
  203. } else {
  204. console.Debug("正在删除旧程序错误...")
  205. if err = os.RemoveAll(filename); err != nil {
  206. plugins.Set2(key, "")
  207. console.Error("删除旧程序错误:%v", err)
  208. return &storage.Final{
  209. Error: fmt.Errorf("删除旧程序错误:%v", err),
  210. }
  211. }
  212. }
  213. console.Debug("正在移动新程序错误...")
  214. if err = os.Rename(ready, filename); err != nil {
  215. console.Error("移动新程序错误:%v", err)
  216. return &storage.Final{
  217. Error: fmt.Errorf("移动新程序错误:%v", err),
  218. }
  219. }
  220. go func() {
  221. console.Debug("正在重启...")
  222. time.Sleep(time.Second)
  223. utils.Daemon()
  224. }()
  225. return &storage.Final{
  226. Message: "升级成功,即将重启!",
  227. }
  228. }
  229. return nil
  230. })
  231. storage.Watch(sillyGirl, "started_at", func(old, new, key string) *storage.Final {
  232. if old != new {
  233. go func() {
  234. time.Sleep(time.Second)
  235. utils.Daemon()
  236. }()
  237. return &storage.Final{
  238. Message: "1秒重启!",
  239. }
  240. }
  241. return nil
  242. })
  243. api_key := sillyGirl.GetString("api_key")
  244. if api_key == "" {
  245. api_key := time.Now().UnixNano()
  246. sillyGirl.Set("api_key", api_key)
  247. }
  248. // if sillyplus.GetString("uuid") == "" {
  249. sillyGirl.Set("uuid", utils.GenUUID())
  250. // }
  251. httplib.SetDefaultSetting(httplib.BeegoHTTPSettings{
  252. ConnectTimeout: time.Second * 10,
  253. ReadWriteTimeout: time.Second * 10,
  254. UserAgent: "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/99.0.4844.51 Safari/537.36",
  255. })
  256. initPlugins()
  257. initReboot()
  258. initListenReply()
  259. // initPluginFile()
  260. initWebPluginList()
  261. go initPluginList()
  262. initPluginPublish()
  263. }