mirror of
https://github.com/v2rayA/v2rayA.git
synced 2026-10-29 05:26:43 +08:00
The iptables path wrote each whitelisted CIDR with its own iptables process. With the mainland China group (about 15,000 CIDRs) a core start spent minutes on this, and every packet of a direct connection walked the list linearly. Load each family into one hash:net set with a single `ipset restore` and match it with one rule at the old position. Without ipset, write the rules with one `iptables-restore --noflush` per family; without that, keep the per-rule commands. Cleanup destroys the sets.
44 lines
1003 B
Go
44 lines
1003 B
Go
package cmds
|
|
|
|
import (
|
|
"fmt"
|
|
"github.com/v2rayA/v2rayA/pkg/util/log"
|
|
"os/exec"
|
|
"strings"
|
|
)
|
|
|
|
func IsCommandValid(command string) bool {
|
|
_, err := exec.LookPath(command)
|
|
return err == nil
|
|
}
|
|
|
|
func ExecCommandWithInput(command string, args []string, input string) error {
|
|
cmd := exec.Command(command, args...)
|
|
cmd.Stdin = strings.NewReader(input)
|
|
out, err := cmd.CombinedOutput()
|
|
if err != nil {
|
|
return fmt.Errorf("ExecCommandWithInput: %s %s: %s: %w", command, strings.Join(args, " "), out, err)
|
|
}
|
|
return nil
|
|
}
|
|
|
|
func ExecCommands(commands string, stopWhenError bool) error {
|
|
lines := strings.Split(commands, "\n")
|
|
var e error
|
|
for _, line := range lines {
|
|
line = strings.TrimSpace(line)
|
|
if len(line) <= 0 || strings.HasPrefix(line, "#") {
|
|
continue
|
|
}
|
|
out, err := exec.Command("sh", "-c", line).CombinedOutput()
|
|
if err != nil {
|
|
e = fmt.Errorf("ExecCommands: %v %v: %w", line, string(out), err)
|
|
if stopWhenError {
|
|
log.Trace("%v", e)
|
|
return e
|
|
}
|
|
}
|
|
}
|
|
return e
|
|
}
|