main.go 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. // Copyright 2019 Yunion
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. package main
  15. import (
  16. "fmt"
  17. "os"
  18. "time"
  19. "yunion.io/x/pkg/errors"
  20. "yunion.io/x/structarg"
  21. "yunion.io/x/onecloud/pkg/hostman/hostinfo/hostdhcp"
  22. "yunion.io/x/onecloud/pkg/util/atexit"
  23. )
  24. type Options struct {
  25. Help bool `help:"Show help"`
  26. Interface string `help:"Listening interface, e.g. eth0"`
  27. Ip string `help:"Listening interface IP, e.g. 192.168.22.2"`
  28. Port int `help:"listening port" default:"67"`
  29. Relay string `help:"Relay server address, e.g. 192.168.22.23"`
  30. Ip6 string `help:"Listening interface IP, e.g. 2001:db8::1"`
  31. Port6 int `help:"listening port" default:"547"`
  32. Relay6 string `help:"Relay server address, e.g. 2001:db8::23"`
  33. }
  34. func relayMain() error {
  35. parse, err := structarg.NewArgumentParser(&Options{},
  36. "dhcprelay",
  37. "An independent dhcp relay.",
  38. `See "dhcprelay --help" for help.`)
  39. if err != nil {
  40. return err
  41. }
  42. err = parse.ParseArgs(os.Args[1:], false)
  43. if err != nil {
  44. return err
  45. }
  46. options := parse.Options().(*Options)
  47. if options.Help {
  48. fmt.Print(parse.HelpString())
  49. return errors.Error("Need help!")
  50. }
  51. if len(options.Interface) == 0 {
  52. return errors.Error("Missing interface")
  53. }
  54. if len(options.Ip) == 0 && len(options.Ip6) == 0 {
  55. return errors.Error("Missing interface IP or IP6")
  56. }
  57. if len(options.Ip) > 0 {
  58. if len(options.Relay) == 0 {
  59. return errors.Error("Missing DHCP relay server or relay server6")
  60. }
  61. relayConfig := &hostdhcp.SDHCPRelayUpstream{}
  62. relayConfig.IP = options.Relay
  63. relayConfig.Port = 67
  64. srv, err := hostdhcp.NewGuestDHCPServer(options.Interface, options.Port, relayConfig)
  65. if err != nil {
  66. return errors.Wrap(err, "NewGuestDHCPServer")
  67. }
  68. srv.Start(false)
  69. srv.RelaySetup(options.Ip)
  70. }
  71. if len(options.Ip6) > 0 {
  72. if len(options.Relay6) == 0 {
  73. return errors.Error("Missing DHCP relay server or relay server6")
  74. }
  75. relayConfig6 := &hostdhcp.SDHCPRelayUpstream{}
  76. relayConfig6.IP = options.Relay6
  77. relayConfig6.Port = options.Port6
  78. srv6, err := hostdhcp.NewGuestDHCP6Server(options.Interface, options.Port6, relayConfig6)
  79. if err != nil {
  80. return errors.Wrap(err, "NewGuestDHCP6Server")
  81. }
  82. srv6.Start(false)
  83. srv6.RelaySetup(options.Ip6)
  84. }
  85. for {
  86. time.Sleep(time.Hour)
  87. }
  88. // return nil
  89. }
  90. func main() {
  91. defer atexit.Handle()
  92. err := relayMain()
  93. if err != nil {
  94. fmt.Fprintf(os.Stdout, "Error: %s", err)
  95. os.Exit(-1)
  96. }
  97. }