|
| 1 | +package cmd |
| 2 | + |
| 3 | +import ( |
| 4 | + "fmt" |
| 5 | + "os" |
| 6 | + "strings" |
| 7 | + "time" |
| 8 | + |
| 9 | + "github.com/apernet/hysteria/core/v2/server" |
| 10 | + "github.com/apernet/hysteria/extras/v2/l2tp" |
| 11 | + "github.com/apernet/hysteria/extras/v2/pppbridge" |
| 12 | + "go.uber.org/zap" |
| 13 | +) |
| 14 | + |
| 15 | +func (c *serverConfig) fillPPPConfig(hyConfig *server.Config) error { |
| 16 | + if !c.PPP.Enabled { |
| 17 | + return nil |
| 18 | + } |
| 19 | + |
| 20 | + mode := strings.ToLower(c.PPP.Mode) |
| 21 | + if mode == "" { |
| 22 | + mode = "local" |
| 23 | + } |
| 24 | + |
| 25 | + switch mode { |
| 26 | + case "local": |
| 27 | + return c.fillPPPConfigLocal(hyConfig) |
| 28 | + case "l2tp": |
| 29 | + return c.fillPPPConfigL2TP(hyConfig) |
| 30 | + default: |
| 31 | + return configError{Field: "ppp.mode", Err: fmt.Errorf("unsupported mode %q (must be \"local\" or \"l2tp\")", c.PPP.Mode)} |
| 32 | + } |
| 33 | +} |
| 34 | + |
| 35 | +func (c *serverConfig) fillPPPConfigLocal(hyConfig *server.Config) error { |
| 36 | + pppdPath := c.PPP.PPPDPath |
| 37 | + if pppdPath == "" { |
| 38 | + pppdPath = "/usr/sbin/pppd" |
| 39 | + } |
| 40 | + |
| 41 | + var pool *pppbridge.IPPool |
| 42 | + if c.PPP.IPv4Pool != "" { |
| 43 | + var err error |
| 44 | + pool, err = pppbridge.NewIPPool(c.PPP.IPv4Pool) |
| 45 | + if err != nil { |
| 46 | + return configError{Field: "ppp.ipv4Pool", Err: err} |
| 47 | + } |
| 48 | + } |
| 49 | + |
| 50 | + poolDesc := "(IPv6-only)" |
| 51 | + if c.PPP.IPv4Pool != "" { |
| 52 | + poolDesc = c.PPP.IPv4Pool |
| 53 | + } |
| 54 | + logger.Info("PPP enabled (local mode)", |
| 55 | + zap.String("ipv4Pool", poolDesc), |
| 56 | + zap.String("pppdPath", pppdPath), |
| 57 | + zap.Bool("sudo", c.PPP.Sudo), |
| 58 | + zap.Uint32("mtu", c.PPP.MTU)) |
| 59 | + |
| 60 | + hyConfig.PPPRequestHandler = &pppbridge.ServerPPPHandler{ |
| 61 | + PPPDPath: pppdPath, |
| 62 | + PPPDArgs: c.PPP.PPPDArgs, |
| 63 | + Sudo: c.PPP.Sudo, |
| 64 | + IPv4Pool: pool, |
| 65 | + DNS: c.PPP.DNS, |
| 66 | + MTU: int(c.PPP.MTU), |
| 67 | + Salamander: strings.EqualFold(c.Obfs.Type, "salamander"), |
| 68 | + Logger: logger, |
| 69 | + } |
| 70 | + return nil |
| 71 | +} |
| 72 | + |
| 73 | +func (c *serverConfig) fillPPPConfigL2TP(hyConfig *server.Config) error { |
| 74 | + l2tpCfg := c.PPP.L2TP |
| 75 | + |
| 76 | + if l2tpCfg.Hostname == "" { |
| 77 | + l2tpCfg.Hostname = os.Getenv("HYSTERIA_LAC_HOSTNAME") |
| 78 | + } |
| 79 | + if l2tpCfg.Hostname == "" { |
| 80 | + l2tpCfg.Hostname, _ = os.Hostname() |
| 81 | + } |
| 82 | + if l2tpCfg.Hostname == "" { |
| 83 | + return configError{Field: "ppp.l2tp.hostname", Err: fmt.Errorf("hostname is required but could not be determined")} |
| 84 | + } |
| 85 | + |
| 86 | + // Validate groups |
| 87 | + if len(l2tpCfg.Groups) == 0 { |
| 88 | + return configError{Field: "ppp.l2tp.groups", Err: fmt.Errorf("at least one LNS group is required")} |
| 89 | + } |
| 90 | + lbGroups := make(map[string][]l2tp.LNSConfig) |
| 91 | + for name, group := range l2tpCfg.Groups { |
| 92 | + if len(group.LNS) == 0 { |
| 93 | + return configError{Field: fmt.Sprintf("ppp.l2tp.groups.%s.lns", name), Err: fmt.Errorf("at least one LNS is required")} |
| 94 | + } |
| 95 | + for _, lns := range group.LNS { |
| 96 | + if lns.Address == "" { |
| 97 | + return configError{Field: fmt.Sprintf("ppp.l2tp.groups.%s.lns.address", name), Err: fmt.Errorf("LNS address is required")} |
| 98 | + } |
| 99 | + w := lns.Weight |
| 100 | + if w <= 0 { |
| 101 | + w = 1 |
| 102 | + } |
| 103 | + lbGroups[name] = append(lbGroups[name], l2tp.LNSConfig{ |
| 104 | + Address: lns.Address, |
| 105 | + Secret: lns.Secret, |
| 106 | + Weight: w, |
| 107 | + }) |
| 108 | + } |
| 109 | + } |
| 110 | + |
| 111 | + // Validate realms |
| 112 | + if len(l2tpCfg.Realms) == 0 { |
| 113 | + return configError{Field: "ppp.l2tp.realms", Err: fmt.Errorf("at least one realm rule is required")} |
| 114 | + } |
| 115 | + var realmRules []l2tp.RealmRule |
| 116 | + for _, realm := range l2tpCfg.Realms { |
| 117 | + if realm.Pattern == "" { |
| 118 | + return configError{Field: "ppp.l2tp.realms.pattern", Err: fmt.Errorf("realm pattern is required")} |
| 119 | + } |
| 120 | + if _, ok := l2tpCfg.Groups[realm.Group]; !ok { |
| 121 | + return configError{Field: "ppp.l2tp.realms.group", Err: fmt.Errorf("realm references unknown group %q", realm.Group)} |
| 122 | + } |
| 123 | + realmRules = append(realmRules, l2tp.RealmRule{ |
| 124 | + Pattern: realm.Pattern, |
| 125 | + Group: realm.Group, |
| 126 | + }) |
| 127 | + } |
| 128 | + |
| 129 | + helloInterval := time.Duration(l2tpCfg.HelloInterval) * time.Second |
| 130 | + |
| 131 | + tm := l2tp.NewTunnelManager(l2tpCfg.Hostname, helloInterval, logger) |
| 132 | + rr := l2tp.NewRealmRouter(realmRules) |
| 133 | + lb := l2tp.NewLoadBalancer(lbGroups) |
| 134 | + |
| 135 | + logger.Info("PPP enabled (L2TP mode)", |
| 136 | + zap.String("hostname", l2tpCfg.Hostname), |
| 137 | + zap.Int("helloInterval", l2tpCfg.HelloInterval), |
| 138 | + zap.Int("groups", len(l2tpCfg.Groups)), |
| 139 | + zap.Int("realms", len(l2tpCfg.Realms))) |
| 140 | + |
| 141 | + hyConfig.PPPRequestHandler = &pppbridge.L2TPPPPHandler{ |
| 142 | + TunnelManager: tm, |
| 143 | + RealmRouter: rr, |
| 144 | + LoadBalancer: lb, |
| 145 | + Logger: logger, |
| 146 | + } |
| 147 | + return nil |
| 148 | +} |
0 commit comments