dify_files.go 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290
  1. package llm
  2. const (
  3. DIFY_SSRF_ENTRYPINT_SHELL = `
  4. mkdir -p /etc/squid
  5. cat > /etc/squid/squid.conf.template <<'EOF'
  6. %s
  7. EOF
  8. echo "[ENTRYPOINT] re-create snakeoil self-signed certificate removed in the build process"
  9. if [ ! -f /etc/ssl/private/ssl-cert-snakeoil.key ]; then
  10. /usr/sbin/make-ssl-cert generate-default-snakeoil --force-overwrite > /dev/null 2>&1
  11. fi
  12. tail -F /var/log/squid/access.log 2>/dev/null &
  13. tail -F /var/log/squid/error.log 2>/dev/null &
  14. tail -F /var/log/squid/store.log 2>/dev/null &
  15. tail -F /var/log/squid/cache.log 2>/dev/null &
  16. echo "[ENTRYPOINT] replacing environment variables in the template"
  17. awk '{
  18. while(match($0, /\${[A-Za-z_][A-Za-z_0-9]*}/)) {
  19. var = substr($0, RSTART+2, RLENGTH-3)
  20. val = ENVIRON[var]
  21. $0 = substr($0, 1, RSTART-1) val substr($0, RSTART+RLENGTH)
  22. }
  23. print
  24. }' /etc/squid/squid.conf.template > /etc/squid/squid.conf
  25. chown -R "$SQUID_USER":"$SQUID_USER" /etc/squid
  26. /usr/sbin/squid -Nz
  27. echo "[ENTRYPOINT] starting squid"
  28. /usr/sbin/squid -f /etc/squid/squid.conf -NYC 1
  29. `
  30. DIFY_SSRF_SQUID_CONFIGURATION_FILE = `visible_hostname localhost # set visible_hostname to avoid WARNING: Could not determine this machines public hostname.
  31. acl localnet src 0.0.0.1-0.255.255.255 # RFC 1122 "this" network (LAN)
  32. acl localnet src 10.0.0.0/8 # RFC 1918 local private network (LAN)
  33. acl localnet src 100.64.0.0/10 # RFC 6598 shared address space (CGN)
  34. acl localnet src 169.254.0.0/16 # RFC 3927 link-local (directly plugged) machines
  35. acl localnet src 172.16.0.0/12 # RFC 1918 local private network (LAN)
  36. acl localnet src 192.168.0.0/16 # RFC 1918 local private network (LAN)
  37. acl localnet src fc00::/7 # RFC 4193 local private network range
  38. acl localnet src fe80::/10 # RFC 4291 link-local (directly plugged) machines
  39. acl SSL_ports port 443
  40. # acl SSL_ports port 1025-65535 # Enable the configuration to resolve this issue: https://github.com/langgenius/dify/issues/12792
  41. acl Safe_ports port 80 # http
  42. acl Safe_ports port 21 # ftp
  43. acl Safe_ports port 443 # https
  44. acl Safe_ports port 70 # gopher
  45. acl Safe_ports port 210 # wais
  46. acl Safe_ports port 1025-65535 # unregistered ports
  47. acl Safe_ports port 280 # http-mgmt
  48. acl Safe_ports port 488 # gss-http
  49. acl Safe_ports port 591 # filemaker
  50. acl Safe_ports port 777 # multiling http
  51. acl CONNECT method CONNECT
  52. acl allowed_domains dstdomain .marketplace.dify.ai
  53. http_access allow allowed_domains
  54. http_access deny !Safe_ports
  55. http_access deny CONNECT !SSL_ports
  56. http_access allow localhost manager
  57. http_access deny manager
  58. http_access allow localhost
  59. include /etc/squid/conf.d/*.conf
  60. http_access deny all
  61. ################################## Proxy Server ################################
  62. http_port ${HTTP_PORT}
  63. coredump_dir ${COREDUMP_DIR}
  64. refresh_pattern ^ftp: 1440 20% 10080
  65. refresh_pattern ^gopher: 1440 0% 1440
  66. refresh_pattern -i (/cgi-bin/|\?) 0 0% 0
  67. refresh_pattern \/(Packages|Sources)(|\.bz2|\.gz|\.xz)$ 0 0% 0 refresh-ims
  68. refresh_pattern \/Release(|\.gpg)$ 0 0% 0 refresh-ims
  69. refresh_pattern \/InRelease$ 0 0% 0 refresh-ims
  70. refresh_pattern \/(Translation-.*)(|\.bz2|\.gz|\.xz)$ 0 0% 0 refresh-ims
  71. refresh_pattern . 0 20% 4320
  72. # cache_dir ufs /var/spool/squid 100 16 256
  73. # upstream proxy, set to your own upstream proxy IP to avoid SSRF attacks
  74. # cache_peer 172.1.1.1 parent 3128 0 no-query no-digest no-netdb-exchange default
  75. ################################## Reverse Proxy To Sandbox ################################
  76. http_port ${REVERSE_PROXY_PORT} accel vhost
  77. cache_peer ${SANDBOX_HOST} parent ${SANDBOX_PORT} 0 no-query originserver
  78. acl src_all src all
  79. http_access allow src_all
  80. # Unless the option's size is increased, an error will occur when uploading more than two files.
  81. client_request_buffer_max_size 100 MB
  82. `
  83. )
  84. const (
  85. DIFY_NGINX_ENTRYPINT_SHELL = `
  86. mkdir -p /etc/nginx
  87. cat > /etc/nginx/nginx.conf.template <<'EOF'
  88. %s
  89. EOF
  90. cat > /etc/nginx/proxy.conf.template <<'EOF'
  91. %s
  92. EOF
  93. cat > /etc/nginx/conf.d/default.conf.template <<'EOF'
  94. %s
  95. EOF
  96. env_vars=$(printenv | cut -d= -f1 | sed 's/^/$/g' | paste -sd, -)
  97. echo "Substituting variables: $env_vars"
  98. envsubst "$env_vars" < /etc/nginx/nginx.conf.template > /etc/nginx/nginx.conf
  99. envsubst "$env_vars" < /etc/nginx/proxy.conf.template > /etc/nginx/proxy.conf
  100. envsubst "$env_vars" < /etc/nginx/conf.d/default.conf.template > /etc/nginx/conf.d/default.conf
  101. exec nginx -g 'daemon off;'`
  102. DIFY_NGINX_NGINX_CONF_FILE = `
  103. user nginx;
  104. worker_processes ${NGINX_WORKER_PROCESSES};
  105. error_log /var/log/nginx/error.log notice;
  106. pid /var/run/nginx.pid;
  107. events {
  108. worker_connections 1024;
  109. }
  110. http {
  111. include /etc/nginx/mime.types;
  112. default_type application/octet-stream;
  113. log_format main '$remote_addr - $remote_user [$time_local] "$request" '
  114. '$status $body_bytes_sent "$http_referer" '
  115. '"$http_user_agent" "$http_x_forwarded_for"';
  116. access_log /var/log/nginx/access.log main;
  117. sendfile on;
  118. #tcp_nopush on;
  119. keepalive_timeout ${NGINX_KEEPALIVE_TIMEOUT};
  120. #gzip on;
  121. client_max_body_size ${NGINX_CLIENT_MAX_BODY_SIZE};
  122. include /etc/nginx/conf.d/*.conf;
  123. }
  124. `
  125. DIFY_NGINX_PROXY_CONF_FILE = `
  126. proxy_set_header Host $host;
  127. proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
  128. proxy_set_header X-Forwarded-Proto $scheme;
  129. proxy_set_header X-Forwarded-Port $server_port;
  130. proxy_http_version 1.1;
  131. proxy_set_header Connection "";
  132. proxy_buffering off;
  133. proxy_read_timeout ${NGINX_PROXY_READ_TIMEOUT};
  134. proxy_send_timeout ${NGINX_PROXY_SEND_TIMEOUT};
  135. `
  136. DIFY_NGINX_DEFAULT_CONF_FILE = `# Please do not directly edit this file. Instead, modify the .env variables related to NGINX configuration.
  137. server {
  138. listen ${NGINX_PORT};
  139. server_name ${NGINX_SERVER_NAME};
  140. location /console/api {
  141. proxy_pass http://localhost:5001;
  142. include proxy.conf;
  143. }
  144. location /api {
  145. proxy_pass http://localhost:5001;
  146. include proxy.conf;
  147. }
  148. location /v1 {
  149. proxy_pass http://localhost:5001;
  150. include proxy.conf;
  151. }
  152. location /files {
  153. proxy_pass http://localhost:5001;
  154. include proxy.conf;
  155. }
  156. location /explore {
  157. proxy_pass http://localhost:3000;
  158. include proxy.conf;
  159. }
  160. location /e/ {
  161. proxy_pass http://localhost:5002;
  162. proxy_set_header Dify-Hook-Url $scheme://$host$request_uri;
  163. include proxy.conf;
  164. }
  165. location / {
  166. proxy_pass http://localhost:3000;
  167. include proxy.conf;
  168. }
  169. location /mcp {
  170. proxy_pass http://localhost:5001;
  171. include proxy.conf;
  172. }
  173. # placeholder for acme challenge location
  174. # ${ACME_CHALLENGE_LOCATION}
  175. # placeholder for https config defined in https.conf.template
  176. # ${HTTPS_CONFIG}
  177. }
  178. `
  179. )
  180. const (
  181. DIFY_SANDBOX_WRITE_CONF_SHELL = `
  182. cat > /conf/config.yaml <<'EOF'
  183. %s
  184. EOF
  185. cat > /conf/config.yaml.template <<'EOF'
  186. %s
  187. EOF
  188. touch /dependencies/python-requirements.txt
  189. # decompress nodejs
  190. tar -xvf $NODE_TAR_XZ -C /opt
  191. ln -s $NODE_DIR/bin/node /usr/local/bin/node
  192. rm -f $NODE_TAR_XZ
  193. # start main
  194. /main
  195. `
  196. DIFY_SANDBOX_CONF_FILE = `app:
  197. port: 8194
  198. debug: True
  199. key: dify-sandbox
  200. max_workers: 4
  201. max_requests: 50
  202. worker_timeout: 5
  203. python_path: /usr/local/bin/python3
  204. enable_network: True # please make sure there is no network risk in your environment
  205. allowed_syscalls: # please leave it empty if you have no idea how seccomp works
  206. proxy:
  207. socks5: ''
  208. http: ''
  209. https: ''
  210. `
  211. DIFY_SANDBOX_CONF_TEMP_FILE = `app:
  212. port: 8194
  213. debug: True
  214. key: dify-sandbox
  215. max_workers: 4
  216. max_requests: 50
  217. worker_timeout: 5
  218. python_path: /usr/local/bin/python3
  219. python_lib_path:
  220. - /usr/local/lib/python3.10
  221. - /usr/lib/python3.10
  222. - /usr/lib/python3
  223. - /usr/lib/x86_64-linux-gnu
  224. - /etc/ssl/certs/ca-certificates.crt
  225. - /etc/nsswitch.conf
  226. - /etc/hosts
  227. - /etc/resolv.conf
  228. - /run/systemd/resolve/stub-resolv.conf
  229. - /run/resolvconf/resolv.conf
  230. - /etc/localtime
  231. - /usr/share/zoneinfo
  232. - /etc/timezone
  233. # add more paths if needed
  234. python_pip_mirror_url: https://pypi.tuna.tsinghua.edu.cn/simple
  235. nodejs_path: /usr/local/bin/node
  236. enable_network: True
  237. allowed_syscalls:
  238. - 1
  239. - 2
  240. - 3
  241. # add all the syscalls which you require
  242. proxy:
  243. socks5: ''
  244. http: ''
  245. https: ''
  246. `
  247. )