klzgrad / naiveproxy

Make a fortune quietly
BSD 3-Clause "New" or "Revised" License
6.49k stars 873 forks source link

Error during parsing: parsing caddyfile tokens for 'forward_proxy': /etc/caddy/Caddyfile:5... #135

Closed mattieross closed 3 years ago

mattieross commented 3 years ago

按照项目主页的示例在服务器编译caddy,运行caddy报错并退出, Caddyfile也是按照example配置,在85版本可以正常运行。 日志如下:

caddy[542]: run: adapting config using caddyfile: parsing caddyfile tokens for 'route': /etc/caddy/Caddyfile:11 - Error during parsing: parsing caddyfile tokens for 'forward_proxy': /etc/caddy/Caddyfile:5 - Error during parsing: Wrong argument count or unexpected line ending after 'password'

systemd[1]: caddy.service: Main process exited, code=exited, status=1/FAILURE

systemd[1]: caddy.service: Failed with result 'exit-code'.
klzgrad commented 3 years ago

Caddy版本跟naive无关

而且你肯定不是按照example配置

因为如果Caddyfile按照example一字不变,可以得到如下结果:

2020/10/10 13:31:18.345 INFO    using adjacent Caddyfile
2020/10/10 13:31:18.346 INFO    admin   admin endpoint started  {"address": "tcp/localhost:2019", "enforce_origin": false, "origins": ["localhost:2019", "[::1]:2019", "127.0.0.1:2019"]}
2020/10/10 13:31:18.346 INFO    http    server is listening only on the HTTPS port but has no TLS connection policies; adding one to enable TLS {"server_name": "srv0", "https_port": 443}
2020/10/10 13:31:18.346 INFO    http    enabling automatic HTTP->HTTPS redirects    {"server_name": "srv0"}
2020/10/10 13:31:18.346 INFO    http.handlers.forward_proxy Secret domain used to connect to proxy: secret.com

并没有你给出的错误

mattieross commented 3 years ago

感谢回复, 我又测试了几遍, 一字不变用Caddyfile还是跑不起来, 然后改用以前的json配置就可以启动caddy,

ArcCal commented 3 years ago

我也试了几次 也有一模一样的问题

GWONGJYULEI commented 3 years ago

I built caddy v2.2.0 with the latest forwardproxy in my virtual ubuntu-20 in PC today, and following klzgrad`s tutorials. There is no error when the self-built caddy run with the Caddyfile as following:

:443, xxx.com
tls xxx@xxx.com
route {
  forward_proxy {
    basic_auth USER PASSWORD
    hide_ip
    hide_via
    probe_resistance xxx.com
  }
  file_server { root /var/www/html }
}

And the following is my building commands:

git clone -b naive https://github.com/klzgrad/forwardproxy
go get -u github.com/caddyserver/xcaddy/cmd/xcaddy
~/go/bin/xcaddy build --with github.com/caddyserver/forwardproxy=$PWD/forwardproxy
klzgrad commented 3 years ago
build@ubuntu:~$ cat /etc/issue.net 
Ubuntu 18.04.5 LTS
build@ubuntu:~$ go version
go version go1.15.2 linux/amd64
build@ubuntu:~$ ~/go/bin/xcaddy build --with github.com/caddyserver/forwardproxy@caddy2=github.com/klzgrad/forwardproxy@naive
2020/10/11 07:21:25 [INFO] Temporary folder: /tmp/buildenv_2020-10-11-0721.824418959
2020/10/11 07:21:25 [INFO] Writing main module: /tmp/buildenv_2020-10-11-0721.824418959/main.go
2020/10/11 07:21:25 [INFO] Initializing Go module
2020/10/11 07:21:25 [INFO] exec (timeout=10s): /usr/bin/go mod init caddy 
go: creating new go.mod: module caddy
2020/10/11 07:21:25 [INFO] Replace github.com/caddyserver/forwardproxy => github.com/klzgrad/forwardproxy@naive
2020/10/11 07:21:25 [INFO] exec (timeout=10s): /usr/bin/go mod edit -replace github.com/caddyserver/forwardproxy=github.com/klzgrad/forwardproxy@naive 
2020/10/11 07:21:25 [INFO] Pinning versions
2020/10/11 07:21:25 [INFO] exec (timeout=0s): /usr/bin/go get -d -v github.com/caddyserver/caddy/v2 
go: downloading github.com/caddyserver/caddy/v2 v2.2.0
go: github.com/caddyserver/caddy/v2 upgrade => v2.2.0
go: downloading github.com/prometheus/client_golang v1.7.1
go: downloading golang.org/x/sys v0.0.0-20200615200032-f1bc736245b1
go: downloading github.com/prometheus/client_model v0.2.0
go: downloading github.com/prometheus/common v0.10.0
go: downloading github.com/beorn7/perks v1.0.1
go: downloading github.com/caddyserver/certmagic v0.12.0
go: downloading github.com/prometheus/procfs v0.1.3
go: downloading github.com/cespare/xxhash/v2 v2.1.1
go: downloading github.com/mholt/acmez v0.1.1
go: downloading github.com/matttproud/golang_protobuf_extensions v1.0.1
2020/10/11 07:21:29 [INFO] Build environment ready
2020/10/11 07:21:29 [INFO] Building Caddy
2020/10/11 07:21:29 [INFO] exec (timeout=0s): /usr/bin/go build -o /tmp/caddy -ldflags -w -s -trimpath 
go: downloading github.com/klzgrad/forwardproxy v0.0.0-20201002032014-d5751cbeedc4
go: downloading github.com/smallstep/certificates v0.15.4
go: downloading github.com/klauspost/compress v1.11.0
go: downloading github.com/smallstep/cli v0.15.2
go: downloading go.step.sm/crypto v0.6.0
go: downloading gopkg.in/square/go-jose.v2 v2.5.1
go: found github.com/caddyserver/forwardproxy in github.com/caddyserver/forwardproxy v0.0.0-00010101000000-000000000000
2020/10/11 07:23:22 [INFO] Build complete: caddy
2020/10/11 07:23:22 [INFO] Cleaning up temporary folder: /tmp/buildenv_2020-10-11-0721.824418959
build@ubuntu:~$ cat Caddyfile
:443, example.com
tls me@example.com
route {
  forward_proxy {
    basic_auth user pass
    hide_ip
    hide_via
    probe_resistance secret.com
  }
  file_server { root /var/www/html }
}
build@ubuntu:~$ ./caddy validate Caddyfile
2020/10/11 11:28:13.896 INFO    using adjacent Caddyfile
2020/10/11 11:28:13.899 INFO    tls.cache.maintenance   started background certificate maintenance  {"cache": "0xc00025cf50"}
2020/10/11 11:28:13.899 INFO    http    server is listening only on the HTTPS port but has no TLS connection policies; adding one to enable TLS {"server_name": "srv0", "https_port": 443}
2020/10/11 11:28:13.899 INFO    http    enabling automatic HTTP->HTTPS redirects    {"server_name": "srv0"}
2020/10/11 11:28:13.900 INFO    http.handlers.forward_proxy Secret domain used to connect to proxy: secret.com
2020/10/11 11:28:13.900 INFO    tls.cache.maintenance   stopped background certificate maintenance  {"cache": "0xc00025cf50"}
Valid configuration
mattieross commented 3 years ago

build输出有点不一样

root@testhost:~# cat /etc/issue.net
Debian GNU/Linux 10
root@testhost:~# go version
go version go1.15.2 linux/amd64
root@testhost:~# ~/go/bin/xcaddy build --with github.com/caddyserver/forwardproxy@caddy2=github.com/klzgrad/forwardproxy@naive
2020/10/11 19:36:07 [INFO] Temporary folder: /tmp/buildenv_2020-10-11-1936.703209018
2020/10/11 19:36:07 [INFO] Writing main module: /tmp/buildenv_2020-10-11-1936.703209018/main.go
2020/10/11 19:36:07 [INFO] Initializing Go module
2020/10/11 19:36:07 [INFO] exec (timeout=10s): /usr/local/go/bin/go mod init caddy 
go: creating new go.mod: module caddy
2020/10/11 19:36:07 [INFO] Replace github.com/caddyserver/forwardproxy => github.com/klzgrad/forwardproxy@naive
2020/10/11 19:36:07 [INFO] exec (timeout=10s): /usr/local/go/bin/go mod edit -replace github.com/caddyserver/forwardproxy=github.com/klzgrad/forwardproxy@naive 
2020/10/11 19:36:07 [INFO] Pinning versions
2020/10/11 19:36:07 [INFO] exec (timeout=0s): /usr/local/go/bin/go get -d -v github.com/caddyserver/caddy/v2 
go: github.com/caddyserver/caddy/v2 upgrade => v2.2.0
2020/10/11 19:36:09 [INFO] Build environment ready
2020/10/11 19:36:09 [INFO] Building Caddy
2020/10/11 19:36:09 [INFO] exec (timeout=0s): /usr/local/go/bin/go build -o /root/caddy -ldflags -w -s -trimpath 
go: found github.com/caddyserver/forwardproxy in github.com/caddyserver/forwardproxy v0.0.0-00010101000000-000000000000
2020/10/11 19:36:40 [INFO] Build complete: caddy
2020/10/11 19:36:40 [INFO] Cleaning up temporary folder: /tmp/buildenv_2020-10-11-1936.703209018
root@testhost:~# ./caddy validate Caddyfile
2020/10/11 11:42:15.268 INFO    using adjacent Caddyfile
2020/10/11 11:42:15.270 INFO    http    server is listening only on the HTTPS port but has no TLS connection policies; adding one to enable TLS {"server_name": "srv0", "https_port": 443}
2020/10/11 11:42:15.271 INFO    http    enabling automatic HTTP->HTTPS redirects    {"server_name": "srv0"}
2020/10/11 11:42:15.272 INFO    http.handlers.forward_proxy Secret domain used to connect to proxy: secret.com
2020/10/11 11:42:15.273 INFO    tls.cache.maintenance   started background certificate maintenance  {"cache": "0xc00021f0a0"}
2020/10/11 11:42:15.274 INFO    tls.cache.maintenance   stopped background certificate maintenance  {"cache": "0xc00021f0a0"}
Valid configuration
mv caddy /usr/bin
chmod +x /usr/bin/caddy && setcap cap_net_bind_service=+ep /usr/bin/caddy
mkdir -p /var/www/html /etc/caddy
nano /etc/caddy/Caddyfile
nano /usr/lib/systemd/system/caddy.service
systemctl start caddy
systemctl status caddy
Oct 11 20:14:37 testhost systemd[1]: Started Caddy.
Oct 11 20:14:38 testhost caddy[542]: {"level":"warn","ts":1602418478.0699658,"msg"
Oct 11 20:14:38 testhost caddy[542]: {"level":"info","ts":1602418478.2570362,"msg"
Oct 11 20:14:38 testhost caddy[542]: run: adapting config using caddyfile: parsing
Oct 11 20:14:38 testhost systemd[1]: caddy.service: Main process exited, code=exit
Oct 11 20:14:38 testhost systemd[1]: caddy.service: Failed with result 'exit-code'
cat /var/log/syslog
Oct 11 20:14:38 testhost caddy[542]: run: adapting config using caddyfile: parsing caddyfile tokens for 'route': /etc/caddy/Caddyfile:11 - Error during parsing: parsing caddyfile tokens for 'forward_proxy': /etc/caddy/Caddyfile:5 - Error during parsing: Wrong argument count or unexpected line ending after 'password'
klzgrad commented 3 years ago

Caddy 2的Caddyfile语法跟1不一样,调整一下Caddyfile

mattieross commented 3 years ago

抱歉,是我的错! basic_auth 写成 basicauth 少了个下划线。