config.go 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588
  1. package config
  2. import (
  3. //"github.com/davecgh/go-spew/spew"
  4. "github.com/urfave/cli/v2"
  5. "os"
  6. "encoding/base64"
  7. "errors"
  8. "fmt"
  9. "log"
  10. "encoding/json"
  11. "gopkg.in/yaml.v3"
  12. nodepb "github.com/gravitl/netmaker/grpc"
  13. "github.com/gravitl/netmaker/models"
  14. )
  15. type GlobalConfig struct {
  16. Client models.IntClient
  17. }
  18. type ClientConfig struct {
  19. Server ServerConfig `yaml:"server"`
  20. Node NodeConfig `yaml:"node"`
  21. Network string `yaml:"network"`
  22. Daemon string `yaml:"daemon"`
  23. OperatingSystem string `yaml:"operatingsystem"`
  24. }
  25. type ServerConfig struct {
  26. GRPCAddress string `yaml:"grpcaddress"`
  27. APIAddress string `yaml:"apiaddress"`
  28. AccessKey string `yaml:"accesskey"`
  29. GRPCSSL string `yaml:"grpcssl"`
  30. GRPCWireGuard string `yaml:"grpcwg"`
  31. }
  32. type ListConfig struct {
  33. Name string `yaml:"name"`
  34. Interface string `yaml:"interface"`
  35. PrivateIPv4 string `yaml:"wgaddress"`
  36. PrivateIPv6 string `yaml:"wgaddress6"`
  37. PublicEndpoint string `yaml:"endpoint"`
  38. }
  39. type NodeConfig struct {
  40. Name string `yaml:"name"`
  41. Interface string `yaml:"interface"`
  42. Network string `yaml:"network"`
  43. Password string `yaml:"password"`
  44. MacAddress string `yaml:"macaddress"`
  45. LocalAddress string `yaml:"localaddress"`
  46. WGAddress string `yaml:"wgaddress"`
  47. WGAddress6 string `yaml:"wgaddress6"`
  48. Roaming string `yaml:"roaming"`
  49. DNS string `yaml:"dns"`
  50. IsLocal string `yaml:"islocal"`
  51. IsDualStack string `yaml:"isdualstack"`
  52. IsIngressGateway string `yaml:"isingressgateway"`
  53. AllowedIPs []string `yaml:"allowedips"`
  54. LocalRange string `yaml:"localrange"`
  55. PostUp string `yaml:"postup"`
  56. PostDown string `yaml:"postdown"`
  57. Port int32 `yaml:"port"`
  58. KeepAlive int32 `yaml:"keepalive"`
  59. PublicKey string `yaml:"publickey"`
  60. ServerPubKey string `yaml:"serverpubkey"`
  61. PrivateKey string `yaml:"privatekey"`
  62. Endpoint string `yaml:"endpoint"`
  63. PostChanges string `yaml:"postchanges"`
  64. StaticIP string `yaml:"staticip"`
  65. StaticPubKey string `yaml:"staticpubkey"`
  66. IPForwarding string `yaml:"ipforwarding"`
  67. }
  68. //reading in the env file
  69. func Write(config *ClientConfig, network string) error{
  70. if network == "" {
  71. err := errors.New("No network provided. Exiting.")
  72. return err
  73. }
  74. _, err := os.Stat("/etc/netclient")
  75. if os.IsNotExist(err) {
  76. os.Mkdir("/etc/netclient", 744)
  77. } else if err != nil {
  78. return err
  79. }
  80. home := "/etc/netclient"
  81. if err != nil {
  82. log.Fatal(err)
  83. }
  84. file := fmt.Sprintf(home + "/netconfig-" + network)
  85. f, err := os.OpenFile(file, os.O_CREATE|os.O_WRONLY|os.O_TRUNC, os.ModePerm)
  86. defer f.Close()
  87. err = yaml.NewEncoder(f).Encode(config)
  88. if err != nil {
  89. return err
  90. }
  91. return err
  92. }
  93. //reading in the env file
  94. func WriteGlobal(config *GlobalConfig) error{
  95. _, err := os.Stat("/etc/netclient")
  96. if os.IsNotExist(err) {
  97. os.Mkdir("/etc/netclient", 744)
  98. } else if err != nil {
  99. return err
  100. }
  101. home := "/etc/netclient"
  102. if err != nil {
  103. log.Fatal(err)
  104. }
  105. file := fmt.Sprintf(home + "/netconfig-global-001")
  106. f, err := os.OpenFile(file, os.O_CREATE|os.O_WRONLY|os.O_TRUNC, os.ModePerm)
  107. defer f.Close()
  108. err = yaml.NewEncoder(f).Encode(config)
  109. if err != nil {
  110. return err
  111. }
  112. return err
  113. }
  114. func WriteServer(server string, accesskey string, network string) error{
  115. if network == "" {
  116. err := errors.New("No network provided. Exiting.")
  117. return err
  118. }
  119. nofile := false
  120. //home, err := homedir.Dir()
  121. _, err := os.Stat("/etc/netclient")
  122. if os.IsNotExist(err) {
  123. os.Mkdir("/etc/netclient", 744)
  124. } else if err != nil {
  125. fmt.Println("couldnt find or create /etc/netclient")
  126. return err
  127. }
  128. home := "/etc/netclient"
  129. file := fmt.Sprintf(home + "/netconfig-" + network)
  130. //f, err := os.Open(file)
  131. f, err := os.OpenFile(file, os.O_CREATE|os.O_RDWR, 0666)
  132. //f, err := ioutil.ReadFile(file)
  133. if err != nil {
  134. fmt.Println("couldnt open netconfig-" + network)
  135. fmt.Println(err)
  136. nofile = true
  137. //err = nil
  138. return err
  139. }
  140. defer f.Close()
  141. //cfg := &ClientConfig{}
  142. var cfg ClientConfig
  143. if !nofile {
  144. fmt.Println("Writing to existing config file at " + home + "/netconfig-" + network)
  145. decoder := yaml.NewDecoder(f)
  146. err = decoder.Decode(&cfg)
  147. //err = yaml.Unmarshal(f, &cfg)
  148. if err != nil {
  149. //fmt.Println(err)
  150. //return err
  151. }
  152. f.Close()
  153. f, err = os.OpenFile(file, os.O_CREATE|os.O_RDWR|os.O_TRUNC, 0666)
  154. if err != nil {
  155. fmt.Println("couldnt open netconfig")
  156. fmt.Println(err)
  157. nofile = true
  158. //err = nil
  159. return err
  160. }
  161. defer f.Close()
  162. if err != nil {
  163. fmt.Println("trouble opening file")
  164. fmt.Println(err)
  165. }
  166. cfg.Server.GRPCAddress = server
  167. cfg.Server.AccessKey = accesskey
  168. err = yaml.NewEncoder(f).Encode(cfg)
  169. //_, err = yaml.Marshal(f, &cfg)
  170. if err != nil {
  171. fmt.Println("trouble encoding file")
  172. return err
  173. }
  174. } else {
  175. fmt.Println("Creating new config file at " + home + "/netconfig-" + network)
  176. cfg.Server.GRPCAddress = server
  177. cfg.Server.AccessKey = accesskey
  178. newf, err := os.Create(home + "/netconfig-" + network)
  179. err = yaml.NewEncoder(newf).Encode(cfg)
  180. defer newf.Close()
  181. if err != nil {
  182. return err
  183. }
  184. }
  185. return err
  186. }
  187. func(config *ClientConfig) ReadConfig() {
  188. nofile := false
  189. //home, err := homedir.Dir()
  190. home := "/etc/netclient"
  191. file := fmt.Sprintf(home + "/netconfig-" + config.Network)
  192. //f, err := os.Open(file)
  193. f, err := os.OpenFile(file, os.O_RDONLY, 0666)
  194. if err != nil {
  195. fmt.Println("trouble opening file")
  196. fmt.Println(err)
  197. nofile = true
  198. //fmt.Println("Could not access " + home + "/.netconfig, proceeding...")
  199. }
  200. defer f.Close()
  201. //var cfg ClientConfig
  202. if !nofile {
  203. decoder := yaml.NewDecoder(f)
  204. err = decoder.Decode(&config)
  205. if err != nil {
  206. fmt.Println("no config or invalid")
  207. fmt.Println(err)
  208. log.Fatal(err)
  209. } else {
  210. //config = cfg
  211. }
  212. }
  213. }
  214. func ModGlobalConfig(cfg models.IntClient) error{
  215. var modconfig GlobalConfig
  216. var err error
  217. if FileExists("/etc/netclient/netconfig-global-001") {
  218. useconfig, err := ReadGlobalConfig()
  219. if err != nil {
  220. return err
  221. }
  222. modconfig = *useconfig
  223. }
  224. if cfg.ServerWGPort != ""{
  225. modconfig.Client.ServerWGPort = cfg.ServerWGPort
  226. }
  227. if cfg.ServerGRPCPort != ""{
  228. modconfig.Client.ServerGRPCPort = cfg.ServerGRPCPort
  229. }
  230. if cfg.ServerAPIPort != ""{
  231. modconfig.Client.ServerAPIPort = cfg.ServerAPIPort
  232. }
  233. if cfg.PublicKey != ""{
  234. modconfig.Client.PublicKey = cfg.PublicKey
  235. }
  236. if cfg.PrivateKey != ""{
  237. modconfig.Client.PrivateKey = cfg.PrivateKey
  238. }
  239. if cfg.ServerPublicEndpoint != ""{
  240. modconfig.Client.ServerPublicEndpoint = cfg.ServerPublicEndpoint
  241. }
  242. if cfg.ServerPrivateAddress != ""{
  243. modconfig.Client.ServerPrivateAddress = cfg.ServerPrivateAddress
  244. }
  245. if cfg.Address != ""{
  246. modconfig.Client.Address = cfg.Address
  247. }
  248. if cfg.Address6 != ""{
  249. modconfig.Client.Address6 = cfg.Address6
  250. }
  251. if cfg.Network != ""{
  252. modconfig.Client.Network = cfg.Network
  253. }
  254. if cfg.ServerKey != ""{
  255. modconfig.Client.ServerKey = cfg.ServerKey
  256. }
  257. if cfg.AccessKey != ""{
  258. modconfig.Client.AccessKey = cfg.AccessKey
  259. }
  260. if cfg.ClientID != ""{
  261. modconfig.Client.ClientID = cfg.ClientID
  262. }
  263. err = WriteGlobal(&modconfig)
  264. return err
  265. }
  266. func ModConfig(node *nodepb.Node) error{
  267. network := node.Nodenetwork
  268. if network == "" {
  269. return errors.New("No Network Provided")
  270. }
  271. var modconfig ClientConfig
  272. var err error
  273. if FileExists("/etc/netclient/netconfig-"+network) {
  274. useconfig, err := ReadConfig(network)
  275. if err != nil {
  276. return err
  277. }
  278. modconfig = *useconfig
  279. }
  280. nodecfg := modconfig.Node
  281. if node.Name != ""{
  282. nodecfg.Name = node.Name
  283. }
  284. if node.Interface != ""{
  285. nodecfg.Interface = node.Interface
  286. }
  287. if node.Nodenetwork != ""{
  288. nodecfg.Network = node.Nodenetwork
  289. }
  290. if node.Macaddress != ""{
  291. nodecfg.MacAddress = node.Macaddress
  292. }
  293. if node.Localaddress != ""{
  294. nodecfg.LocalAddress = node.Localaddress
  295. }
  296. if node.Postup != ""{
  297. nodecfg.PostUp = node.Postup
  298. }
  299. if node.Postdown != ""{
  300. nodecfg.PostDown = node.Postdown
  301. }
  302. if node.Listenport != 0{
  303. nodecfg.Port = node.Listenport
  304. }
  305. if node.Keepalive != 0{
  306. nodecfg.KeepAlive = node.Keepalive
  307. }
  308. if node.Publickey != ""{
  309. nodecfg.PublicKey = node.Publickey
  310. }
  311. if node.Endpoint != ""{
  312. nodecfg.Endpoint = node.Endpoint
  313. }
  314. if node.Password != ""{
  315. nodecfg.Password = node.Password
  316. }
  317. if node.Address != ""{
  318. nodecfg.WGAddress = node.Address
  319. }
  320. if node.Address6 != ""{
  321. nodecfg.WGAddress6 = node.Address6
  322. }
  323. if node.Postchanges != "" {
  324. nodecfg.PostChanges = node.Postchanges
  325. }
  326. if node.Dnsoff == true {
  327. nodecfg.DNS = "off"
  328. }
  329. if node.Isdualstack == true {
  330. nodecfg.IsDualStack = "yes"
  331. }
  332. if node.Isingressgateway {
  333. nodecfg.IsIngressGateway = "yes"
  334. } else {
  335. nodecfg.IsIngressGateway = "no"
  336. }
  337. if node.Localrange != "" && node.Islocal {
  338. nodecfg.IsLocal = "yes"
  339. nodecfg.LocalRange = node.Localrange
  340. }
  341. modconfig.Node = nodecfg
  342. err = Write(&modconfig, network)
  343. return err
  344. }
  345. func GetCLIConfig(c *cli.Context) (ClientConfig, error){
  346. var cfg ClientConfig
  347. if c.String("token") != "" {
  348. tokenbytes, err := base64.StdEncoding.DecodeString(c.String("token"))
  349. if err != nil {
  350. log.Println("error decoding token")
  351. return cfg, err
  352. }
  353. var accesstoken models.AccessToken
  354. if err := json.Unmarshal(tokenbytes, &accesstoken); err != nil {
  355. log.Println("error converting token json to object", tokenbytes )
  356. return cfg, err
  357. }
  358. if accesstoken.ServerConfig.APIConnString != "" {
  359. cfg.Server.APIAddress = accesstoken.ServerConfig.APIConnString
  360. } else {
  361. cfg.Server.APIAddress = accesstoken.ServerConfig.APIHost
  362. if accesstoken.ServerConfig.APIPort != "" {
  363. cfg.Server.APIAddress = cfg.Server.APIAddress + ":" + accesstoken.ServerConfig.APIPort
  364. }
  365. }
  366. if accesstoken.ServerConfig.GRPCConnString != "" {
  367. cfg.Server.GRPCAddress = accesstoken.ServerConfig.GRPCConnString
  368. } else {
  369. cfg.Server.GRPCAddress = accesstoken.ServerConfig.GRPCHost
  370. if accesstoken.ServerConfig.GRPCPort != "" {
  371. cfg.Server.GRPCAddress = cfg.Server.GRPCAddress + ":" + accesstoken.ServerConfig.GRPCPort
  372. }
  373. }
  374. cfg.Network = accesstoken.ClientConfig.Network
  375. cfg.Node.Network = accesstoken.ClientConfig.Network
  376. cfg.Server.AccessKey = accesstoken.ClientConfig.Key
  377. cfg.Node.LocalRange = accesstoken.ClientConfig.LocalRange
  378. cfg.Server.GRPCSSL = accesstoken.ServerConfig.GRPCSSL
  379. cfg.Server.GRPCWireGuard = accesstoken.WG.GRPCWireGuard
  380. if c.String("grpcserver") != "" {
  381. cfg.Server.GRPCAddress = c.String("grpcserver")
  382. }
  383. if c.String("apiserver") != "" {
  384. cfg.Server.APIAddress = c.String("apiserver")
  385. }
  386. if c.String("key") != "" {
  387. cfg.Server.AccessKey = c.String("key")
  388. }
  389. if c.String("network") != "all" {
  390. cfg.Network = c.String("network")
  391. cfg.Node.Network = c.String("network")
  392. }
  393. if c.String("localrange") != "" {
  394. cfg.Node.LocalRange = c.String("localrange")
  395. }
  396. if c.String("grpcssl") != "" {
  397. cfg.Server.GRPCSSL = c.String("grpcssl")
  398. }
  399. if c.String("grpcwg") != "" {
  400. cfg.Server.GRPCWireGuard = c.String("grpcwg")
  401. }
  402. } else {
  403. cfg.Server.GRPCAddress = c.String("grpcserver")
  404. cfg.Server.APIAddress = c.String("apiserver")
  405. cfg.Server.AccessKey = c.String("key")
  406. cfg.Network = c.String("network")
  407. cfg.Node.Network = c.String("network")
  408. cfg.Node.LocalRange = c.String("localrange")
  409. cfg.Server.GRPCWireGuard = c.String("grpcwg")
  410. cfg.Server.GRPCSSL = c.String("grpcssl")
  411. }
  412. cfg.Node.Name = c.String("name")
  413. cfg.Node.Interface = c.String("interface")
  414. cfg.Node.Password = c.String("password")
  415. cfg.Node.MacAddress = c.String("macaddress")
  416. cfg.Node.LocalAddress = c.String("localaddress")
  417. cfg.Node.WGAddress = c.String("address")
  418. cfg.Node.WGAddress6 = c.String("addressIPV6")
  419. cfg.Node.Roaming = c.String("roaming")
  420. cfg.Node.DNS = c.String("dns")
  421. cfg.Node.IsLocal = c.String("islocal")
  422. cfg.Node.IsDualStack = c.String("isdualstack")
  423. cfg.Node.PostUp = c.String("postup")
  424. cfg.Node.PostDown = c.String("postdown")
  425. cfg.Node.Port = int32(c.Int("port"))
  426. cfg.Node.KeepAlive = int32(c.Int("keepalive"))
  427. cfg.Node.PublicKey = c.String("publickey")
  428. cfg.Node.PrivateKey = c.String("privatekey")
  429. cfg.Node.Endpoint = c.String("endpoint")
  430. cfg.Node.IPForwarding = c.String("ipforwarding")
  431. cfg.OperatingSystem = c.String("operatingsystem")
  432. cfg.Daemon = c.String("daemon")
  433. return cfg, nil
  434. }
  435. func GetCLIConfigRegister(c *cli.Context) (GlobalConfig, error){
  436. var cfg GlobalConfig
  437. if c.String("token") != "" {
  438. tokenbytes, err := base64.StdEncoding.DecodeString(c.String("token"))
  439. if err != nil {
  440. log.Println("error decoding token")
  441. return cfg, err
  442. }
  443. var accesstoken models.AccessToken
  444. if err := json.Unmarshal(tokenbytes, &accesstoken); err != nil {
  445. log.Println("error converting token json to object", tokenbytes )
  446. return cfg, err
  447. }
  448. cfg.Client.ServerPrivateAddress = accesstoken.WG.GRPCWGAddress
  449. cfg.Client.ServerGRPCPort = accesstoken.WG.GRPCWGPort
  450. if err != nil {
  451. log.Println("error decoding token grpcserver")
  452. return cfg, err
  453. }
  454. if err != nil {
  455. log.Println("error decoding token apiserver")
  456. return cfg, err
  457. }
  458. if accesstoken.ServerConfig.APIConnString != "" {
  459. cfg.Client.ServerPublicEndpoint = accesstoken.ServerConfig.APIConnString
  460. } else {
  461. cfg.Client.ServerPublicEndpoint = accesstoken.ServerConfig.APIHost
  462. if accesstoken.ServerConfig.APIPort != "" {
  463. cfg.Client.ServerAPIPort = accesstoken.ServerConfig.APIPort
  464. }
  465. }
  466. cfg.Client.ServerWGPort = accesstoken.WG.GRPCWGPort
  467. cfg.Client.ServerKey = accesstoken.ClientConfig.Key
  468. cfg.Client.ServerKey = accesstoken.WG.GRPCWGPubKey
  469. if c.String("grpcserver") != "" {
  470. cfg.Client.ServerPrivateAddress = c.String("grpcserver")
  471. }
  472. if c.String("apiserver") != "" {
  473. cfg.Client.ServerPublicEndpoint = c.String("apiserver")
  474. }
  475. if c.String("pubkey") != "" {
  476. cfg.Client.ServerKey = c.String("pubkey")
  477. }
  478. if c.String("network") != "all" {
  479. cfg.Client.Network = c.String("network")
  480. }
  481. } else {
  482. cfg.Client.ServerPrivateAddress = c.String("grpcserver")
  483. cfg.Client.ServerPublicEndpoint = c.String("apiserver")
  484. cfg.Client.ServerKey = c.String("key")
  485. cfg.Client.Network = c.String("network")
  486. }
  487. cfg.Client.Address = c.String("address")
  488. cfg.Client.Address6 = c.String("addressIPV6")
  489. cfg.Client.PublicKey = c.String("pubkey")
  490. cfg.Client.PrivateKey = c.String("privkey")
  491. return cfg, nil
  492. }
  493. func ReadConfig(network string) (*ClientConfig, error) {
  494. if network == "" {
  495. err := errors.New("No network provided. Exiting.")
  496. return nil, err
  497. }
  498. nofile := false
  499. home := "/etc/netclient"
  500. file := fmt.Sprintf(home + "/netconfig-" + network)
  501. f, err := os.Open(file)
  502. if err != nil {
  503. nofile = true
  504. }
  505. defer f.Close()
  506. var cfg ClientConfig
  507. if !nofile {
  508. decoder := yaml.NewDecoder(f)
  509. err = decoder.Decode(&cfg)
  510. if err != nil {
  511. fmt.Println("trouble decoding file")
  512. return nil, err
  513. }
  514. }
  515. return &cfg, err
  516. }
  517. func ReadGlobalConfig() (*GlobalConfig, error) {
  518. nofile := false
  519. home := "/etc/netclient"
  520. file := fmt.Sprintf(home + "/netconfig-global-001")
  521. f, err := os.Open(file)
  522. if err != nil {
  523. nofile = true
  524. }
  525. defer f.Close()
  526. var cfg GlobalConfig
  527. if !nofile {
  528. decoder := yaml.NewDecoder(f)
  529. err = decoder.Decode(&cfg)
  530. if err != nil {
  531. fmt.Println("trouble decoding file")
  532. return nil, err
  533. }
  534. }
  535. return &cfg, err
  536. }
  537. func FileExists(f string) bool {
  538. info, err := os.Stat(f)
  539. if os.IsNotExist(err) {
  540. return false
  541. }
  542. return !info.IsDir()
  543. }