b.liu | e958203 | 2025-04-17 19:18:16 +0800 | [diff] [blame^] | 1 | #!/bin/sh /etc/rc.common |
| 2 | # Copyright (C) 2015 OpenWrt.org |
| 3 | |
| 4 | START=90 |
| 5 | STOP=10 |
| 6 | |
| 7 | USE_PROCD=1 |
| 8 | PROG=/usr/sbin/squid |
| 9 | CONFIGFILE="/tmp/squid/squid.conf" |
| 10 | MIMETABLE="/tmp/squid/mime.conf" |
| 11 | |
| 12 | validate_squid_section() { |
| 13 | uci_load_validate squid squid "$1" "$2" \ |
| 14 | 'config_file:string' \ |
| 15 | 'http_port:port:3128' \ |
| 16 | 'http_port_options:string' \ |
| 17 | 'ssldb:string' \ |
| 18 | 'ssldb_options:string' \ |
| 19 | 'coredump_dir:string' \ |
| 20 | 'visible_hostname:string:OpenWrt' \ |
| 21 | 'pinger_enable:string:off' \ |
| 22 | 'mime_table:string:/etc/squid/mime.conf' |
| 23 | } |
| 24 | |
| 25 | create_squid_user() { |
| 26 | user_exists squid || user_add squid $USERID |
| 27 | group_exists squid || group_add squid $USERID && group_add_user squid squid |
| 28 | } |
| 29 | |
| 30 | start_squid_instance() { |
| 31 | local config_dir |
| 32 | |
| 33 | [ "$2" = 0 ] || { |
| 34 | echo "validation failed" |
| 35 | return 1 |
| 36 | } |
| 37 | |
| 38 | config_dir=$(dirname $CONFIGFILE) |
| 39 | [ -d $config_dir ] || mkdir -p $config_dir && chown nobody:nogroup $config_dir |
| 40 | [ -d $coredump_dir ] || mkdir -p $coredump_dir && chown nobody:nogroup $coredump_dir |
| 41 | [ "$ssldb" ] && ( [ -f "$ssldb"/size ] || /usr/lib/squid/security_file_certgen -c -s $ssldb $ssldb_options && chown -R nobody:nogroup $ssldb ) |
| 42 | |
| 43 | cat $config_file > $CONFIGFILE |
| 44 | echo http_port $http_port $http_port_options >> $CONFIGFILE |
| 45 | echo coredump_dir $coredump_dir >> $CONFIGFILE |
| 46 | echo visible_hostname $visible_hostname >> $CONFIGFILE |
| 47 | echo pinger_enable $pinger_enable >> $CONFIGFILE |
| 48 | cat $mime_table > $MIMETABLE |
| 49 | echo mime_table $MIMETABLE >> $CONFIGFILE |
| 50 | [ "$ssldb" ] && echo sslcrtd_program /usr/lib/squid/security_file_certgen -s $ssldb $ssldb_options >> $CONFIGFILE |
| 51 | $PROG -s -f $CONFIGFILE -N -z 2>/dev/null |
| 52 | |
| 53 | procd_open_instance |
| 54 | procd_set_param command $PROG -s -f $CONFIGFILE -N |
| 55 | procd_set_param file $CONFIGFILE |
| 56 | procd_set_param respawn |
| 57 | procd_close_instance |
| 58 | } |
| 59 | |
| 60 | start_service() |
| 61 | { |
| 62 | validate_squid_section squid start_squid_instance |
| 63 | } |
| 64 | |
| 65 | stop_service() |
| 66 | { |
| 67 | $PROG -f $CONFIGFILE -N -k shutdown 2>/dev/null |
| 68 | } |
| 69 | |
| 70 | service_triggers() |
| 71 | { |
| 72 | procd_add_reload_trigger "squid" |
| 73 | procd_add_validation validate_squid_section |
| 74 | } |