# Send real ip to application for fail2ban

**URL:** <https://openziti.discourse.group/t/send-real-ip-to-application-for-fail2ban/2484>\
**Category:** zrok\
**Created:** [April 28, 2024, 4:23pm UTC](https://openziti.discourse.group/t/send-real-ip-to-application-for-fail2ban/2484 "2024-04-28T16:23:20Z")\
**Posts on this page:** 20\
**Page:** 2

<div class="post-metadata">

**Author:** ![aikooo7](https://yyz2.discourse-cdn.com/free1/user_avatar/openziti.discourse.group/aikooo7/32/2014_2.png) [@aikooo7](https://openziti.discourse.group/u/aikooo7)\
**Post date:** [June 10, 2024, 2:50pm UTC](https://openziti.discourse.group/t/send-real-ip-to-application-for-fail2ban/2484/22 "2024-06-10T14:50:50Z")

</div>

Hello @qrkourier,

I was able to see my ip by that method, but when I go and boot up my forgejo and the zrok and look to the logs I get a infrastructure ip, not mine, does someone with some caddy knowledge be able to help me?

---

<div class="post-metadata">

**Author:** ![qrkourier](https://yyz2.discourse-cdn.com/free1/user_avatar/openziti.discourse.group/qrkourier/32/52_2.png) [@qrkourier](https://openziti.discourse.group/u/qrkourier)\
**Post date:** [June 10, 2024, 6:21pm UTC](https://openziti.discourse.group/t/send-real-ip-to-application-for-fail2ban/2484/23 "2024-06-10T18:21:13Z")

</div>

Thanks for mentioning Forgejo. I didn't realize there was an even free-er alternative to Gitea. 🙂

Do you know how Forgejo is programmed to determine the client IP address?

I understand you were able to inspect the HTTP headers that are being sent to the backend by running the test endpoint and visiting it in a web browser.

I think the next step is to configure or program Forgejo to reference one of the available HTTP headers to determine the client IP address.

---

<div class="post-metadata">

**Author:** ![aikooo7](https://yyz2.discourse-cdn.com/free1/user_avatar/openziti.discourse.group/aikooo7/32/2014_2.png) [@aikooo7](https://openziti.discourse.group/u/aikooo7)\
**Post date:** [June 10, 2024, 8:58pm UTC](https://openziti.discourse.group/t/send-real-ip-to-application-for-fail2ban/2484/24 "2024-06-10T20:58:48Z")

</div>

Hi,

> [@qrkourier](#):
>
> Thanks for mentioning Forgejo. I didn't realize there was an even free-er alternative to Gitea. 🙂

No worries, always good to help some brothers out 🙂

> [@qrkourier](#):
>
> I think the next step is to configure or program Forgejo to reference one of the available HTTP headers to determine the client IP address.

I didn't look in the source code but since it is using the zrok's infrastructure ip it either means it using the last value of X-Forwarded-For or it is ignoring it completely and using the ip of who does a request but for my testing I tried bitwarden and it also gave me the infrastructure's ip so I assume it using the X-Forwarded-For

---

<div class="post-metadata">

**Author:** ![qrkourier](https://yyz2.discourse-cdn.com/free1/user_avatar/openziti.discourse.group/qrkourier/32/52_2.png) [@qrkourier](https://openziti.discourse.group/u/qrkourier)\
**Post date:** [June 10, 2024, 10:36pm UTC](https://openziti.discourse.group/t/send-real-ip-to-application-for-fail2ban/2484/25 "2024-06-10T22:36:49Z")

</div>

For sure. It really comes down to precisely how those apps determine the client IP. You've noted the most likely possibilities, and all zrok can do is report that IP in some header(s). If the other apps are reverse-proxy-friendly, they probably have a way to configure an HTTP header or documentation, at least, to let you know which header needs setting.

You're using zrok's Caddy backend mode, so you some additional flexibility to handle the HTTP header with your Caddyfile template (the target file for `--backend-mode caddy`).

For example, if Forgejo happens to require some HTTP header that zrok doesn't set, you may be able to set it in your Caddyfile.

Good luck. Let me know how it goes.

---

<div class="post-metadata">

**Author:** ![aikooo7](https://yyz2.discourse-cdn.com/free1/user_avatar/openziti.discourse.group/aikooo7/32/2014_2.png) [@aikooo7](https://openziti.discourse.group/u/aikooo7)\
**Post date:** [June 22, 2024, 12:47am UTC](https://openziti.discourse.group/t/send-real-ip-to-application-for-fail2ban/2484/26 "2024-06-22T00:47:20Z")

</div>

Hey,

Looking at the [docs of forgejo for reverso proxy](https://forgejo.org/docs/latest/admin/reverse-proxy/) I found that is the nginx needed stuff:

```nginx
proxy_set_header Connection $http_connection;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto $scheme;

```

So that means X-Real-Ip is what is uses?

---

<div class="post-metadata">

**Author:** ![aikooo7](https://yyz2.discourse-cdn.com/free1/user_avatar/openziti.discourse.group/aikooo7/32/2014_2.png) [@aikooo7](https://openziti.discourse.group/u/aikooo7)\
**Post date:** [June 22, 2024, 1:03am UTC](https://openziti.discourse.group/t/send-real-ip-to-application-for-fail2ban/2484/27 "2024-06-22T01:03:12Z")

</div>

Update:

Log from normal query:

```auto
2024/06/21 21:22:11 ...eb/routing/logger.go:68:func1() [I] router: polling GET /user/events for XXX.XX.X.X:X, elapsed 3918.5ms @ events/events.go:18(events.Events)

```

Log with zrok:

```auto
2024/06/22 01:56:27 ...eb/routing/logger.go:102:func1() [I] router: completed POST /user/login for [ziti-edge-router connId=2147483649, logical=ziti-sdk[router=tls:ad2b1ffe-8cee-465d-b3fc-8d1100bb5f32.production.netfoundry.io:443]]:0, 200 OK in 366.2ms @ auth/auth.go:193(auth.SignInPost) 2024/06/22 01:58:21 ...s/actions/cleanup.go:29:CleanupArtifacts() [I] Found 0 expired artifacts

```

Caddyfile:

```Caddy
{                                                                               
    debug                                                                   
}
                                                                                                                                                       http:// {                                                                       
    bind {{ .ZrokBindAddress }}                                                        
    header {                                                                                  
        Strict-Transport-Security "max-age=31536000; 
        includeSubdomains"
        Content-Security-Policy "frame-ancestors 'none';  
        object-src 'none'"         
        X-Frame-Options "DENY"
        X-Content-Type-Options "nosniff"                                        
     }                                                                           
     reverse_proxy forgejo:3000 {                                                    
         header_up X-Real-IP {remote}                                                       
     }                                                                      
}

```

---

<div class="post-metadata">

**Author:** ![qrkourier](https://yyz2.discourse-cdn.com/free1/user_avatar/openziti.discourse.group/qrkourier/32/52_2.png) [@qrkourier](https://openziti.discourse.group/u/qrkourier)\
**Post date:** [June 24, 2024, 12:51pm UTC](https://openziti.discourse.group/t/send-real-ip-to-application-for-fail2ban/2484/28 "2024-06-24T12:51:26Z")

</div>

Nice going. Does `{remote}` (the client IP:port pair) give you what you need for fail2ban to parse the logs?

If fail2ban is keying on the IP:port pair then you might need to use [Caddyfile placeholder](https://caddyserver.com/docs/caddyfile/concepts#placeholders)`{remote_host}` (short for `{http.request.remote.host}`) instead of `{remote}` (short for `{http.request.remote}`).

---

<div class="post-metadata">

**Author:** ![aikooo7](https://yyz2.discourse-cdn.com/free1/user_avatar/openziti.discourse.group/aikooo7/32/2014_2.png) [@aikooo7](https://openziti.discourse.group/u/aikooo7)\
**Post date:** [June 25, 2024, 9:35pm UTC](https://openziti.discourse.group/t/send-real-ip-to-application-for-fail2ban/2484/29 "2024-06-25T21:35:27Z")

</div>

Hey,

Crowdsec parses the ip from the service log using a regex sort of way.

With your suggestion, I get this log:

```auto
OK in 87.6ms @ web/home.go:32(web.Home)
2024/06/25 22:34:02 ...eb/routing/logger.go:102:func1() [I] router: completed GET / for [ziti-edge-router connId=2147483654, logical=ziti-sdk[router=tls:f12da3a5-c651-4dbe-ace0-67723a5dda65.production.netfoundry.io:443]]:0, 200 OK in 49.2ms @ web/home.go:32(web.Home)

```

---

<div class="post-metadata">

**Author:** ![qrkourier](https://yyz2.discourse-cdn.com/free1/user_avatar/openziti.discourse.group/qrkourier/32/52_2.png) [@qrkourier](https://openziti.discourse.group/u/qrkourier)\
**Post date:** [June 26, 2024, 2:06am UTC](https://openziti.discourse.group/t/send-real-ip-to-application-for-fail2ban/2484/30 "2024-06-26T02:06:59Z")

</div>

Is that message emitted by a zrok public share process? I think you're trying to parse the real IP of the client that sent a request to a public share into Crowdsec.

---

<div class="post-metadata">

**Author:** ![aikooo7](https://yyz2.discourse-cdn.com/free1/user_avatar/openziti.discourse.group/aikooo7/32/2014_2.png) [@aikooo7](https://openziti.discourse.group/u/aikooo7)\
**Post date:** [June 30, 2024, 4:24pm UTC](https://openziti.discourse.group/t/send-real-ip-to-application-for-fail2ban/2484/31 "2024-06-30T16:24:23Z")

</div>

That message is what I get in my forgejo log while using zrok public share, and yes I would like to instead get the ip of the person accessing so i.e if they bruteforcing I can just ban them

---

<div class="post-metadata">

**Author:** ![qrkourier](https://yyz2.discourse-cdn.com/free1/user_avatar/openziti.discourse.group/qrkourier/32/52_2.png) [@qrkourier](https://openziti.discourse.group/u/qrkourier)\
**Post date:** [June 30, 2024, 4:52pm UTC](https://openziti.discourse.group/t/send-real-ip-to-application-for-fail2ban/2484/32 "2024-06-30T16:52:20Z")

</div>

I see the same thing. If I proxy the request to the zrok test endpoint with the same Caddyfile then I get X-Real-Ip header of the Ziti Go SDK's router connection ID, not the client IP address.

```auto
X-Real-Ip	ziti-edge-router connId=2147483648, logical=ziti-sdk[router=tls:77cb8af4-b721-434d-aaf8-2582a6fce48d.production.netfoundry.io:443]

```

Changing the Caddyfile to use placeholder `{http.request.header.x-forwarded-for}`:

```caddyfile
     reverse_proxy forgejo:3000 {                                                    
         header_up X-Real-IP {http.request.header.x-forwarded-for}
     }                                                                      

```

I now get the IP from which the public share received the request in the first comma-sep position (e.g., 1.2.3.4).

```auto
X-Real-Ip	1.2.3.4, 10.51.129.128

```

It's unclear where the second IP came from, but maybe forgejo will use this comma-sep'd list correctly if it appears in the expected header.

---

<div class="post-metadata">

**Author:** ![aikooo7](https://yyz2.discourse-cdn.com/free1/user_avatar/openziti.discourse.group/aikooo7/32/2014_2.png) [@aikooo7](https://openziti.discourse.group/u/aikooo7)\
**Post date:** [July 1, 2024, 8:22pm UTC](https://openziti.discourse.group/t/send-real-ip-to-application-for-fail2ban/2484/33 "2024-07-01T20:22:32Z")

</div>

_ **Thanks a lot** _ it worked, [~~I also saw that someone~~ That someone is you! Thank you for doing it... made a commit explaining how to do what you just told me](https://github.com/openziti/zrok/commit/952aae5e7a18bca2dcaf3a6bb8eca8e1bb4cd6a5), I hope it helps anyone with the same issue.

Two thing,sorry for so many question but forgejo also supports git using ssh, how can I expose the port 22? Compiling the module l4 could be a option but since that is unlikely to happen, I would also think that I can run two docker's, one with the caddy file and other using backend tcptunnel, is that possible?

And also, is it possible to use the crowdsec module with the caddy?

---

<div class="post-metadata">

**Author:** ![qrkourier](https://yyz2.discourse-cdn.com/free1/user_avatar/openziti.discourse.group/qrkourier/32/52_2.png) [@qrkourier](https://openziti.discourse.group/u/qrkourier)\
**Post date:** [July 1, 2024, 9:09pm UTC](https://openziti.discourse.group/t/send-real-ip-to-application-for-fail2ban/2484/34 "2024-07-01T21:09:02Z")

</div>

You're welcome!

You've got two alternatives for pushing commits to forgejo:

1. create an "app" token in your profile/security settings and use it as the password with a Git credential helper like `store`, `cache`, `pass`, etc to push to the HTTP Git remote
2. create a second zrok share with mode tcpTunnel and push to the private access address as an SSH Git remote like `git@localhost:9191:myproject/myrepo.git`

Each zrok share is a container. They can all share the same environment in a docker volume or bind.

---

<div class="post-metadata">

**Author:** ![aikooo7](https://yyz2.discourse-cdn.com/free1/user_avatar/openziti.discourse.group/aikooo7/32/2014_2.png) [@aikooo7](https://openziti.discourse.group/u/aikooo7)\
**Post date:** [July 1, 2024, 10:23pm UTC](https://openziti.discourse.group/t/send-real-ip-to-application-for-fail2ban/2484/35 "2024-07-01T22:23:21Z")

</div>

> [@qrkourier](#):
>
> I now get the IP from which the public share received the request in the first comma-sep position (e.g., 1.2.3.4).
> 
> ```auto
> X-Real-Ip	1.2.3.4, 10.51.129.128
> 
> ```
> 
> It's unclear where the second IP came from, but maybe forgejo will use this comma-sep'd list correctly if it appears in the expected header.

I couldn't remove the second ip but I found this:

> <https://serverfault.com/questions/846489/can-x-forwarded-for-contain-multiple-ips/846492#846492>

> Yes, if a request is chained through more than one proxy server, then each proxy should add the IP of the preceding one to the existing X-Forwarded-For header so that the entire chain is preserved.

---

<div class="post-metadata">

**Author:** ![aikooo7](https://yyz2.discourse-cdn.com/free1/user_avatar/openziti.discourse.group/aikooo7/32/2014_2.png) [@aikooo7](https://openziti.discourse.group/u/aikooo7)\
**Post date:** [July 6, 2024, 1:34pm UTC](https://openziti.discourse.group/t/send-real-ip-to-application-for-fail2ban/2484/36 "2024-07-06T13:34:41Z")

</div>

Could the two shares point to the same subdomain? So I could do git push to the same domain I use in the browser?

---

<div class="post-metadata">

**Author:** ![qrkourier](https://yyz2.discourse-cdn.com/free1/user_avatar/openziti.discourse.group/qrkourier/32/52_2.png) [@qrkourier](https://openziti.discourse.group/u/qrkourier)\
**Post date:** [July 7, 2024, 3:21pm UTC](https://openziti.discourse.group/t/send-real-ip-to-application-for-fail2ban/2484/37 "2024-07-07T15:21:03Z")

</div>

You're suggesting two zrok shares, one with proxy backend for HTTP requests, and one with a tcpTunnel backend for SSH. Both have a target of the same address, but with different ports. That'll work just fine!

---

<div class="post-metadata">

**Author:** ![aikooo7](https://yyz2.discourse-cdn.com/free1/user_avatar/openziti.discourse.group/aikooo7/32/2014_2.png) [@aikooo7](https://openziti.discourse.group/u/aikooo7)\
**Post date:** [July 13, 2024, 12:19am UTC](https://openziti.discourse.group/t/send-real-ip-to-application-for-fail2ban/2484/38 "2024-07-13T00:19:19Z")

</div>

[ERROR] invalid sharing mode for a public share: tcpTunnel

---

<div class="post-metadata">

**Author:** ![qrkourier](https://yyz2.discourse-cdn.com/free1/user_avatar/openziti.discourse.group/qrkourier/32/52_2.png) [@qrkourier](https://openziti.discourse.group/u/qrkourier)\
**Post date:** [July 13, 2024, 12:54am UTC](https://openziti.discourse.group/t/send-real-ip-to-application-for-fail2ban/2484/39 "2024-07-13T00:54:28Z")

</div>

The public share modes all use HTTP.

Public shares support the modes reported by running `zrok share public --help`, and likewise, private share modes are listed in `zrok share private --help`.

However, you can make a private share's access port listen on a public IP address if you set up your own DNS, etc: [Personalized Frontend | Zrok](https://docs.zrok.io/docs/guides/self-hosting/personalized-frontend/)

---

<div class="post-metadata">

**Author:** ![aikooo7](https://yyz2.discourse-cdn.com/free1/user_avatar/openziti.discourse.group/aikooo7/32/2014_2.png) [@aikooo7](https://openziti.discourse.group/u/aikooo7)\
**Post date:** [July 13, 2024, 1:43am UTC](https://openziti.discourse.group/t/send-real-ip-to-application-for-fail2ban/2484/40 "2024-07-13T01:43:03Z")

</div>

I guess than I can't use ssh, too bad to me

---

<div class="post-metadata">

**Author:** ![qrkourier](https://yyz2.discourse-cdn.com/free1/user_avatar/openziti.discourse.group/qrkourier/32/52_2.png) [@qrkourier](https://openziti.discourse.group/u/qrkourier)\
**Post date:** [July 13, 2024, 1:56am UTC](https://openziti.discourse.group/t/send-real-ip-to-application-for-fail2ban/2484/41 "2024-07-13T01:56:42Z")

</div>

You've got a couple of options to use SSH with zrok. Both require running zrok on both client and server.

You'd run a `zrok share private` on the server, and `zrok access private` on the client.

You can use either `tcpTunnel` or `vpn` mode for SSH. You could do it wish `socks` backend mode too, technically.

[Previous page](https://openziti.discourse.group/t/send-real-ip-to-application-for-fail2ban/2484.md?page=1)

[Next page](https://openziti.discourse.group/t/send-real-ip-to-application-for-fail2ban/2484.md?page=3)
