Skip to content

Commit

Permalink
add self cron fea.
Browse files Browse the repository at this point in the history
  • Loading branch information
kkkgo committed Oct 18, 2023
1 parent e6864b7 commit 5dec223
Show file tree
Hide file tree
Showing 5 changed files with 55 additions and 92 deletions.
2 changes: 0 additions & 2 deletions FILES/usr/bin/cron.sh

This file was deleted.

39 changes: 19 additions & 20 deletions FILES/usr/bin/ppg.sh
Original file line number Diff line number Diff line change
Expand Up @@ -55,11 +55,6 @@ fast_node_sel() {
touch /tmp/allnode.failed
fi
}
kill_cron() {
if ps | grep -v "grep" | grep "/etc/cron"; then
/etc/init.d/cron stop
fi
}
kill_clash_cache() {
if [ -f /etc/config/clash/cache.db ]; then
rm -f /etc/config/clash/cache.db
Expand Down Expand Up @@ -544,19 +539,16 @@ reload_gw() {
rm "/tmp/clash.yaml"
fi
if [ "$mode" = "socks5" ]; then
kill_cron
sed -i "s/{clashmode}/rule/g" /tmp/clash_base.yaml
sed 's/\r$//' /etc/config/clash/socks5.yaml >/tmp/clash_socks5.yaml
sed -i "s/{socks5_ip}/$socks5_ip/g" /tmp/clash_socks5.yaml
sed -i "s/{socks5_port}/$socks5_port/g" /tmp/clash_socks5.yaml
ppgw -input /tmp/clash_socks5.yaml -input /tmp/clash_base.yaml -output /tmp/clash.yaml
fi
if [ "$mode" = "yaml" ]; then
kill_cron
try_conf "$yamlfile" "yaml"
fi
if [ "$mode" = "ovpn" ]; then
kill_cron
try_conf "$ovpnfile" "ovpn"
sed -i "s/{clashmode}/direct/g" /tmp/clash_base.yaml
sed -i "s/#interface-name/interface-name/g" /tmp/clash_base.yaml
Expand All @@ -574,8 +566,6 @@ reload_gw() {
else
get_conf "$suburl" "yaml"
fi
ppgw -interval "$subtime"
/etc/init.d/cron reload
else
log "Bad suburl" warn
fi
Expand All @@ -598,7 +588,6 @@ reload_gw() {
fi

if [ "$mode" = "free" ]; then
kill_cron
sed -i "s/{clashmode}/direct/g" /tmp/clash_base.yaml
cat /tmp/clash_base.yaml >/tmp/clash.yaml
fi
Expand Down Expand Up @@ -630,19 +619,11 @@ if [ "$1" = "reload" ]; then
exit
fi

if [ "$1" = "cron" ]; then
if [ -f /tmp/ppgw.ini ]; then
. /tmp/ppgw.ini 2>/dev/tty0
fi
get_conf "$suburl" "yaml"
reload_gw
exit
fi
sysctl -w net.ipv6.conf.all.disable_ipv6=1
net_ready
cat /etc/banner >/dev/tty0
echo " " >/dev/tty0

sleep_count=0
last_hash="empty"
last_yaml_hash="empty"
last_ovpn_hash="empty"
Expand Down Expand Up @@ -768,5 +749,23 @@ while true; do
else
log "Same hash. Sleep ""$sleeptime""s."
sleep "$sleeptime"
sleep_count=$((sleep_count + 1))
fi
if [ "$mode" = "suburl" ]; then
if [ -f /tmp/ppgw.ini ]; then
. /tmp/ppgw.ini 2>/dev/tty0
fi
if [ -z "$subtime" ]; then
subtime="1d"
fi
# ppgw apply subtime
ppgw_subtime=$(ppgw -interval "$subtime" -sleeptime "$sleeptime")
log "[NEXT SUB-TIME] ""$sleep_count""/""$ppgw_subtime"""
if [ "$sleep_count" -ge "$ppgw_subtime" ]; then
# apply ppgw_subtime
sleep_count=0
get_conf "$suburl" "yaml"
reload_gw
fi
fi
done
6 changes: 1 addition & 5 deletions FILES/usr/bin/reload
Original file line number Diff line number Diff line change
@@ -1,8 +1,4 @@
if [ -f /tmp/ppgw.ini ]; then
. /tmp/ppgw.ini
fi
if [ "$mode" = "suburl" ]; then
/usr/bin/cron.sh
else
/usr/bin/ppg.sh reload
fi
/usr/bin/ppg.sh reload &
6 changes: 5 additions & 1 deletion custom.config.sh
Original file line number Diff line number Diff line change
Expand Up @@ -28,4 +28,8 @@ CONFIG_SIGNATURE_CHECK=n
CONFIG_CLEAN_IPKG=y
CONFIG_KERNEL_IPV6_PIMSM_V2=n
CONFIG_STRIP_KERNEL_EXPORTS=n
CONFIG_PACKAGE_openwrt-keyring=n
CONFIG_PACKAGE_openwrt-keyring=n

CONFIG_BUSYBOX_DEFAULT_CRONTAB=n
BUSYBOX_CONFIG_CROND=n
BUSYBOX_DEFAULT_CROND=n
94 changes: 30 additions & 64 deletions ppgw.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ var (
outputFile string
yamlhashFile string
interval string
sleeptime string
apiURL string
secret string
testNodeURL string
Expand Down Expand Up @@ -136,6 +137,7 @@ func main() {
flag.StringVar(&downURL, "downURL", "", "downURL")
flag.StringVar(&server, "server", "", "DNS server to use")
flag.StringVar(&interval, "interval", "", "sub interval")
flag.StringVar(&sleeptime, "sleeptime", "", "sleeptime")
flag.StringVar(&testProxy, "testProxy", "", "http testProxy")
flag.StringVar(&dnslist, "dnslist", "", "dnslist")
flag.IntVar(&port, "port", 53, "DNS port")
Expand Down Expand Up @@ -424,13 +426,9 @@ func main() {
}
}
//gen_cron
if interval != "" {
err := updateCrontab(interval)
if err != nil {
fmt.Printf("Error updating crontab: %v\n", err)
return
}
fmt.Println("Crontab updated successfully.")
if interval != "" && sleeptime != "" {
result := parseSubtime(interval, sleeptime)
fmt.Printf("%d", result)
os.Exit(0)
}
//gen yaml hash
Expand Down Expand Up @@ -739,68 +737,36 @@ func nslookup(domain string) string {
ip := r[0].IP.String()
return ip
}
func updateCrontab(interval string) error {
interval = strings.ToLower(interval)
cronExpression, err := convertToCronExpression(interval)
if err != nil {
return err
}
return writeCrontab(cronExpression)
}

func convertToCronExpression(interval string) (string, error) {
lastChar := interval[len(interval)-1]
durationPart := interval[:len(interval)-1]
var totalMinutes int

switch lastChar {
case 'm':
duration, err := strconv.Atoi(durationPart)
if err != nil {
return "", err
}
totalMinutes = duration
case 'h':
duration, err := strconv.Atoi(durationPart)
if err != nil {
return "", err
}
totalMinutes = duration * 60
case 'd':
duration, err := strconv.Atoi(durationPart)
if err != nil {
return "", err
func parseSubtime(subtime, sleeptime string) int {
duration := 86400
if subtime != "" {
if strings.HasSuffix(subtime, "d") {
nStr := strings.TrimSuffix(subtime, "d")
n, err := strconv.Atoi(nStr)
if err == nil {
duration = n * 86400
}
} else if strings.HasSuffix(subtime, "h") {
nStr := strings.TrimSuffix(subtime, "h")
n, err := strconv.Atoi(nStr)
if err == nil {
duration = n * 3600
}
} else if strings.HasSuffix(subtime, "m") {
nStr := strings.TrimSuffix(subtime, "m")
n, err := strconv.Atoi(nStr)
if err == nil {
duration = n * 60
}
}
totalMinutes = duration * 24 * 60
default:
return "", fmt.Errorf("invalid time interval")
}

switch {
case totalMinutes >= 24*60:
days := totalMinutes / (24 * 60)
return fmt.Sprintf("0 0 */%d * *", days), nil
case totalMinutes > 60:
hours := totalMinutes / 60
return fmt.Sprintf("0 */%d * * *", hours), nil
case totalMinutes > 0:
return fmt.Sprintf("*/%d * * * *", totalMinutes), nil
default:
return "", fmt.Errorf("invalid time interval")
}
}

func writeCrontab(cronExpression string) error {
filePath := "/etc/crontabs/root"
err := os.WriteFile(filePath, []byte(""), 0644)
sleeptimeInt, err := strconv.Atoi(sleeptime)
if err != nil {
return err
sleeptimeInt = 30
}
err = os.WriteFile(filePath, []byte(cronExpression+" /usr/bin/cron.sh\n"), 0644)
if err != nil {
return err
}
return nil
result := duration / sleeptimeInt
return result
}

func initDNS() {
Expand Down

0 comments on commit 5dec223

Please sign in to comment.