start.go 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  1. package main
  2. import (
  3. "log"
  4. "net/http"
  5. "os"
  6. "strconv"
  7. "strings"
  8. "time"
  9. "imuslab.com/zoraxy/mod/auth"
  10. "imuslab.com/zoraxy/mod/database"
  11. "imuslab.com/zoraxy/mod/dynamicproxy/redirection"
  12. "imuslab.com/zoraxy/mod/ganserv"
  13. "imuslab.com/zoraxy/mod/geodb"
  14. "imuslab.com/zoraxy/mod/mdns"
  15. "imuslab.com/zoraxy/mod/sshprox"
  16. "imuslab.com/zoraxy/mod/statistic"
  17. "imuslab.com/zoraxy/mod/tlscert"
  18. )
  19. /*
  20. Startup Sequence
  21. This function starts the startup sequence of all
  22. required modules
  23. */
  24. var (
  25. /*
  26. MDNS related
  27. */
  28. previousmdnsScanResults = []*mdns.NetworkHost{}
  29. mdnsTickerStop chan bool
  30. )
  31. func startupSequence() {
  32. //Create database
  33. db, err := database.NewDatabase("sys.db", false)
  34. if err != nil {
  35. log.Fatal(err)
  36. }
  37. sysdb = db
  38. //Create tables for the database
  39. sysdb.NewTable("settings")
  40. //Create tmp folder
  41. os.MkdirAll("./tmp", 0775)
  42. //Create an auth agent
  43. sessionKey, err := auth.GetSessionKey(sysdb)
  44. if err != nil {
  45. log.Fatal(err)
  46. }
  47. authAgent = auth.NewAuthenticationAgent(name, []byte(sessionKey), sysdb, true, func(w http.ResponseWriter, r *http.Request) {
  48. //Not logged in. Redirecting to login page
  49. http.Redirect(w, r, ppf("/login.html"), http.StatusTemporaryRedirect)
  50. })
  51. //Create a TLS certificate manager
  52. tlsCertManager, err = tlscert.NewManager("./certs")
  53. if err != nil {
  54. panic(err)
  55. }
  56. //Create a redirection rule table
  57. redirectTable, err = redirection.NewRuleTable("./rules")
  58. if err != nil {
  59. panic(err)
  60. }
  61. //Create a geodb store
  62. geodbStore, err = geodb.NewGeoDb(sysdb)
  63. if err != nil {
  64. panic(err)
  65. }
  66. //Create a statistic collector
  67. statisticCollector, err = statistic.NewStatisticCollector(statistic.CollectorOption{
  68. Database: sysdb,
  69. })
  70. if err != nil {
  71. panic(err)
  72. }
  73. if err != nil {
  74. panic(err)
  75. }
  76. /*
  77. MDNS Discovery Service
  78. This discover nearby ArozOS Nodes or other services
  79. that provide mDNS discovery with domain (e.g. Synology NAS)
  80. */
  81. portInt, err := strconv.Atoi(strings.Split(handler.Port, ":")[1])
  82. if err != nil {
  83. portInt = 8000
  84. }
  85. mdnsScanner, err = mdns.NewMDNS(mdns.NetworkHost{
  86. HostName: "zoraxy_" + nodeUUID,
  87. Port: portInt,
  88. Domain: "zoraxy.imuslab.com",
  89. Model: "Network Gateway",
  90. UUID: nodeUUID,
  91. Vendor: "imuslab.com",
  92. BuildVersion: version,
  93. }, "")
  94. if err != nil {
  95. panic(err)
  96. }
  97. //Start initial scanning
  98. go func() {
  99. hosts := mdnsScanner.Scan(30, "")
  100. previousmdnsScanResults = hosts
  101. log.Println("mDNS Startup scan completed")
  102. }()
  103. //Create a ticker to update mDNS results every 5 minutes
  104. ticker := time.NewTicker(15 * time.Minute)
  105. stopChan := make(chan bool)
  106. go func() {
  107. for {
  108. select {
  109. case <-stopChan:
  110. ticker.Stop()
  111. case <-ticker.C:
  112. hosts := mdnsScanner.Scan(30, "")
  113. previousmdnsScanResults = hosts
  114. log.Println("mDNS scan result updated")
  115. }
  116. }
  117. }()
  118. mdnsTickerStop = stopChan
  119. /*
  120. Global Area Network
  121. Require zerotier token to work
  122. */
  123. usingZtAuthToken := *ztAuthToken
  124. if usingZtAuthToken == "" {
  125. usingZtAuthToken, err = ganserv.TryLoadorAskUserForAuthkey()
  126. if err != nil {
  127. log.Println("Failed to load ZeroTier controller API authtoken")
  128. }
  129. }
  130. ganManager = ganserv.NewNetworkManager(&ganserv.NetworkManagerOptions{
  131. AuthToken: usingZtAuthToken,
  132. ApiPort: *ztAPIPort,
  133. Database: sysdb,
  134. })
  135. //Create WebSSH Manager
  136. webSshManager = sshprox.NewSSHProxyManager()
  137. //Create WoL MAC storage table
  138. sysdb.NewTable("wolmac")
  139. //Create an email sender if SMTP config exists
  140. sysdb.NewTable("smtp")
  141. EmailSender = loadSMTPConfig()
  142. }