soulbalz / traefik-real-ip

When traefik is deployed behind a load balancer, it should get the real IP from the X-Forwarded-For or Cf-Connecting-Ip (if from Cloudflare) header.
MIT License
37 stars 25 forks source link

IP Returned Incorrectly #5

Open droans opened 2 years ago

droans commented 2 years ago

Plugin returns IP as ", 123.123.123.123" instead of just "123.123.123.123"

Configuration:

Docker

traefik:
  container_name: traefik
  image: traefik:brie
  restart: unless-stopped
  command:
     - --experimental.plugins.traefik-real-ip.modulename=github.com/soulbalz/traefik-real-ip
     - --experimental.plugins.traefik-real-ip.version=v1.0.3

middlewares.toml:

[http.middlewares.my-traefik-real-ip]
  [http.middlewares.my-traefik-real-ip.plugin]
    [http.middlewares.my-traefik-real-ip.plugin.traefik-real-ip]
      excludednets = ["192.168.86.0/24", "10.10.0.0/16","172.0.0.0/8"]

Test:

Home Assistant: Invalid IP address in X-Forwarded For: , 192.168.XX.XX

Whats My IP Container result: , 192.168.XX.XX

Same issue with setting excludednets equal to 0.0.0.0/32, 0.0.0.0/0, or any of the individual options above. Seems to work fine for any external IP addresses, just internal causes issues.

yfhyou commented 2 years ago

As it shows in the description, this plugin writes the X-Real-Ip header, not the 'x-forwarded-for' header. You should check the x-real-ip result instead when using this plugin.

This plugin solves this issue by overwriting the X-Real-Ip with an IP from the X-Forwarded-For or Cf-Connecting-Ip (if from Cloudflare) header.

droans commented 2 years ago

As it shows in the description, this plugin writes the X-Real-Ip header, not the 'x-forwarded-for' header. You should check the x-real-ip result instead when using this plugin.

This plugin solves this issue by overwriting the X-Real-Ip with an IP from the X-Forwarded-For or Cf-Connecting-Ip (if from Cloudflare) header.

https://github.com/soulbalz/traefik-real-ip/blob/4ba5e8695b363b7601ab5162a8c4b3899c601504/real_ip.go#L70

The plugin also sets the X-Forwarded-For based on the Cf-Connecting-Ip if X-Real-IP can't be pulled from X-Forwarded-For.

yfhyou commented 2 years ago

I see what you mean. I set up a test that didn't include cf-connection-ip and I get a similar result , 1.2.3.4

simon-sudo commented 2 years ago

same here, I used another plugin and it worked well

https://github.com/Paxxs/traefik-get-real-ip

bverwijst commented 2 years ago

@simon-sudo mind if I ask you how you configured Cloudflare? I'm struggling with configuring the rule expression in the Cloudflare rule to rewrite the header. I have the same issue with just Home Assistant, I'm having the same result with the extra , like the results above.

simon-sudo commented 2 years ago

@bverwijst

add this plugin to the configuration file:

experimental:
  plugins:
    real-ip:
      moduleName: github.com/Paxxs/traefik-get-real-ip
      version: [Please fill the latest version !]

set two values like this:

http:
  middlewares:
    real-ip-foo:
      plugin:
        real-ip:
          Proxy:
            - proxyHeadername: X-From-Cdn
              proxyHeadervalue: cf-my
              realIP: Cf-Connecting-Ip
              OverwriteXFF: true
            - proxyHeadername: "*"
              realIP: RemoteAddr

image

image

image

Reference : https://github.com/Paxxs/traefik-get-real-ip#cdn-configuration

bverwijst commented 2 years ago

@simon-sudo Brilliant, the expression was all I wanted to know, the rest I had correct. Much much appreciated, working like a charm now!

joggs commented 1 year ago

Plugin returns IP as ", 123.123.123.123" instead of just "123.123.123.123"

Configuration:

Docker

traefik:
  container_name: traefik
  image: traefik:brie
  restart: unless-stopped
  command:
     - --experimental.plugins.traefik-real-ip.modulename=github.com/soulbalz/traefik-real-ip
     - --experimental.plugins.traefik-real-ip.version=v1.0.3

middlewares.toml:

[http.middlewares.my-traefik-real-ip]
  [http.middlewares.my-traefik-real-ip.plugin]
    [http.middlewares.my-traefik-real-ip.plugin.traefik-real-ip]
      excludednets = ["192.168.86.0/24", "10.10.0.0/16","172.0.0.0/8"]

Test:

Home Assistant: Invalid IP address in X-Forwarded For: , 192.168.XX.XX

Whats My IP Container result: , 192.168.XX.XX

Same issue with setting excludednets equal to 0.0.0.0/32, 0.0.0.0/0, or any of the individual options above. Seems to work fine for any external IP addresses, just internal causes issues.

Can confirm. Noticed that I could not access home assistant from lan after enabling this plugin. The error log displays "Invalid IP address in X-Forwarded-For: , 192.168.0.58"

gionag commented 1 year ago

same for me. when used from the internal site it fails exactly as @youngt2 and @droans