You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

118 lines
3.1 KiB

  1. {%
  2. function get_local_verdict() {
  3. let v = o_local_default;
  4. if (v == "checkdst") {
  5. return "goto ss_rules_dst_" + proto;
  6. } else if (v == "forward") {
  7. return "goto ss_rules_forward_" + proto;
  8. } else {
  9. return null;
  10. }
  11. }
  12. function get_src_default_verdict() {
  13. let v = o_src_default;
  14. if (v == "checkdst") {
  15. return "goto ss_rules_dst_" + proto;
  16. } else if (v == "forward") {
  17. return "goto ss_rules_forward_" + proto;
  18. } else {
  19. return "accept";
  20. }
  21. }
  22. function get_dst_default_verdict() {
  23. let v = o_dst_default;
  24. if (v == "forward") {
  25. return "goto ss_rules_forward_" + proto;
  26. } else {
  27. return "accept";
  28. }
  29. }
  30. function get_ifnames() {
  31. let res = [];
  32. for (let ifname in split(o_ifnames, /[ \t\n]/)) {
  33. ifname = trim(ifname);
  34. if (ifname) push(res, ifname);
  35. }
  36. return res;
  37. }
  38. let type, hook, priority, redir_port;
  39. if (proto == "tcp") {
  40. type = "nat";
  41. hook = "prerouting";
  42. priority = -1;
  43. redir_port = o_redir_tcp_port;
  44. } else if (proto == "udp") {
  45. type = "filter";
  46. hook = "prerouting";
  47. priority = "mangle";
  48. redir_port = o_redir_udp_port;
  49. if (system("
  50. set -o errexit
  51. while ip rule del fwmark 1 lookup 100 2>/dev/null; do true; done
  52. ip rule add fwmark 1 lookup 100
  53. ip route flush table 100 2>/dev/null || true
  54. ip route add local default dev lo table 100
  55. ") != 0) {
  56. return ;
  57. }
  58. } else {
  59. return;
  60. }
  61. %}
  62. {% if (redir_port): %}
  63. chain ss_rules_pre_{{ proto }} {
  64. type {{ type }} hook {{ hook }} priority {{ priority }};
  65. meta l4proto {{ proto }}{%- let ifnames=get_ifnames(); if (length(ifnames)): %} iifname { {{join(", ", ifnames)}} }{% endif %} goto ss_rules_pre_src_{{ proto }};
  66. }
  67. chain ss_rules_pre_src_{{ proto }} {
  68. ip daddr @ss_rules_dst_bypass_ accept;
  69. ip6 daddr @ss_rules6_dst_bypass_ accept;
  70. goto ss_rules_src_{{ proto }};
  71. }
  72. chain ss_rules_src_{{ proto }} {
  73. ip saddr @ss_rules_src_bypass accept;
  74. ip saddr @ss_rules_src_forward goto ss_rules_forward_{{ proto }};
  75. ip saddr @ss_rules_src_checkdst goto ss_rules_dst_{{ proto }};
  76. ip6 saddr @ss_rules6_src_bypass accept;
  77. ip6 saddr @ss_rules6_src_forward goto ss_rules_forward_{{ proto }};
  78. ip6 saddr @ss_rules6_src_checkdst goto ss_rules_dst_{{ proto }};
  79. {{ get_src_default_verdict() }};
  80. }
  81. chain ss_rules_dst_{{ proto }} {
  82. ip daddr @ss_rules_dst_bypass accept;
  83. ip daddr @ss_rules_dst_forward goto ss_rules_forward_{{ proto }};
  84. ip6 daddr @ss_rules6_dst_bypass accept;
  85. ip6 daddr @ss_rules6_dst_forward goto ss_rules_forward_{{ proto }};
  86. {{ get_dst_default_verdict() }};
  87. }
  88. {% if (proto == "tcp"): %}
  89. chain ss_rules_forward_{{ proto }} {
  90. meta l4proto tcp {{ o_nft_tcp_extra }} redirect to :{{ redir_port }};
  91. }
  92. {% let local_verdict = get_local_verdict(); if (local_verdict): %}
  93. chain ss_rules_local_out {
  94. type {{ type }} hook output priority -1;
  95. meta l4proto != tcp accept;
  96. ip daddr @ss_rules_dst_bypass_ accept;
  97. ip daddr @ss_rules_dst_bypass accept;
  98. ip6 daddr @ss_rules6_dst_bypass_ accept;
  99. ip6 daddr @ss_rules6_dst_bypass accept;
  100. {{ local_verdict }};
  101. }
  102. {% endif %}
  103. {% elif (proto == "udp"): %}
  104. chain ss_rules_forward_{{ proto }} {
  105. meta l4proto udp {{ o_nft_udp_extra }} meta mark set 1 tproxy to :{{ redir_port }};
  106. }
  107. {% endif %}
  108. {% endif %}