|
5 | 5 | "strings" |
6 | 6 | "time" |
7 | 7 |
|
| 8 | + "github.com/1Panel-dev/1Panel/agent/global" |
8 | 9 | "github.com/1Panel-dev/1Panel/agent/utils/cmd" |
9 | 10 | ) |
10 | 11 |
|
@@ -117,6 +118,120 @@ func LoadDefaultStrategy(chain string) (string, error) { |
117 | 118 | return ACCEPT, nil |
118 | 119 | } |
119 | 120 |
|
| 121 | +func LoadInitStatus(clientName, tab string) (bool, bool) { |
| 122 | + if clientName == "firewalld" { |
| 123 | + return true, true |
| 124 | + } |
| 125 | + if clientName == "ufw" && tab != "forward" { |
| 126 | + return true, true |
| 127 | + } |
| 128 | + switch tab { |
| 129 | + case "base": |
| 130 | + filterRules, err := RunWithStd(FilterTab, "-S") |
| 131 | + if err != nil { |
| 132 | + return false, false |
| 133 | + } |
| 134 | + lines := strings.Split(filterRules, "\n") |
| 135 | + initRules := []string{ |
| 136 | + "-N " + Chain1PanelBasicBefore, |
| 137 | + "-N " + Chain1PanelBasic, |
| 138 | + "-N " + Chain1PanelBasicAfter, |
| 139 | + fmt.Sprintf("-A %s %s -j ACCEPT", Chain1PanelBasicBefore, strings.ReplaceAll(strings.ReplaceAll(IoRuleIn, "'", "\""), " -j ACCEPT", "")), |
| 140 | + fmt.Sprintf("-A %s %s -j ACCEPT", Chain1PanelBasicBefore, strings.ReplaceAll(strings.ReplaceAll(EstablishedRule, "'", "\""), " -j ACCEPT", "")), |
| 141 | + fmt.Sprintf("-A %s %s", Chain1PanelBasicAfter, DropAllTcp), |
| 142 | + fmt.Sprintf("-A %s %s", Chain1PanelBasicAfter, DropAllUdp), |
| 143 | + } |
| 144 | + bindRules := []string{ |
| 145 | + fmt.Sprintf("-A %s -j %s", ChainInput, Chain1PanelBasicBefore), |
| 146 | + fmt.Sprintf("-A %s -j %s", ChainInput, Chain1PanelBasic), |
| 147 | + fmt.Sprintf("-A %s -j %s", ChainInput, Chain1PanelBasicAfter), |
| 148 | + } |
| 149 | + return checkWithInitAndBind(initRules, bindRules, lines) |
| 150 | + case "advance": |
| 151 | + filterRules, err := RunWithStd(FilterTab, "-S") |
| 152 | + if err != nil { |
| 153 | + return false, false |
| 154 | + } |
| 155 | + lines := strings.Split(filterRules, "\n") |
| 156 | + initRules := []string{ |
| 157 | + "-N " + Chain1PanelInput, |
| 158 | + "-N " + Chain1PanelOutput, |
| 159 | + } |
| 160 | + bindRules := []string{ |
| 161 | + fmt.Sprintf("-A %s -j %s", ChainInput, Chain1PanelInput), |
| 162 | + fmt.Sprintf("-A %s -j %s", ChainOutput, Chain1PanelOutput), |
| 163 | + } |
| 164 | + return checkWithInitAndBind(initRules, bindRules, lines) |
| 165 | + case "forward": |
| 166 | + stdout, err := cmd.RunDefaultWithStdoutBashC("cat /proc/sys/net/ipv4/ip_forward") |
| 167 | + if err != nil { |
| 168 | + global.LOG.Errorf("check /proc/sys/net/ipv4/ip_forward failed, err: %v", err) |
| 169 | + return false, false |
| 170 | + } |
| 171 | + if strings.TrimSpace(stdout) == "0" { |
| 172 | + return false, false |
| 173 | + } |
| 174 | + natRules, err := RunWithStd(NatTab, "-S") |
| 175 | + if err != nil { |
| 176 | + return false, false |
| 177 | + } |
| 178 | + lines := strings.Split(natRules, "\n") |
| 179 | + initRules := []string{ |
| 180 | + "-N " + Chain1PanelPreRouting, |
| 181 | + "-N " + Chain1PanelPostRouting, |
| 182 | + } |
| 183 | + bindRules := []string{ |
| 184 | + fmt.Sprintf("-A PREROUTING -j %s", Chain1PanelPreRouting), |
| 185 | + fmt.Sprintf("-A POSTROUTING -j %s", Chain1PanelPostRouting), |
| 186 | + } |
| 187 | + isNatInit, isNatBind := checkWithInitAndBind(initRules, bindRules, lines) |
| 188 | + if !isNatInit { |
| 189 | + return false, false |
| 190 | + } |
| 191 | + filterRules, err := RunWithStd(FilterTab, "-S") |
| 192 | + if err != nil { |
| 193 | + return false, false |
| 194 | + } |
| 195 | + filterLines := strings.Split(filterRules, "\n") |
| 196 | + filterInitRules := []string{"-N " + Chain1PanelForward} |
| 197 | + filterBindRules := []string{fmt.Sprintf("-A FORWARD -j %s", Chain1PanelForward)} |
| 198 | + isFilterInit, isFilterBind := checkWithInitAndBind(filterInitRules, filterBindRules, filterLines) |
| 199 | + return isNatInit && isFilterInit, isNatBind && isFilterBind |
| 200 | + default: |
| 201 | + return false, false |
| 202 | + } |
| 203 | +} |
| 204 | + |
| 205 | +func checkWithInitAndBind(initRules, bindRules []string, lines []string) (bool, bool) { |
| 206 | + for _, rule := range initRules { |
| 207 | + found := false |
| 208 | + for _, line := range lines { |
| 209 | + if strings.TrimSpace(line) == strings.TrimSpace(rule) { |
| 210 | + found = true |
| 211 | + break |
| 212 | + } |
| 213 | + } |
| 214 | + if !found { |
| 215 | + global.LOG.Debugf("not found init rule: %s", rule) |
| 216 | + return false, false |
| 217 | + } |
| 218 | + } |
| 219 | + for _, rule := range bindRules { |
| 220 | + found := false |
| 221 | + for _, line := range lines { |
| 222 | + if strings.TrimSpace(line) == strings.TrimSpace(rule) { |
| 223 | + found = true |
| 224 | + break |
| 225 | + } |
| 226 | + } |
| 227 | + if !found { |
| 228 | + global.LOG.Debugf("not found bind rule: %s", rule) |
| 229 | + return true, false |
| 230 | + } |
| 231 | + } |
| 232 | + return true, true |
| 233 | +} |
| 234 | + |
120 | 235 | func loadPort(position string, portStr []string) string { |
121 | 236 | if len(portStr) < 7 { |
122 | 237 | return "" |
|
0 commit comments