Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Create block to display external IP address information #1366

Merged
merged 6 commits into from
Dec 22, 2021

Conversation

cfsmp3
Copy link
Contributor

@cfsmp3 cfsmp3 commented Dec 5, 2021

Display our external IP address and information about it (country, region, location, ISP, etc).

  • To test
[[block]]
block = "external_ip"
format = "{address} {country} {country_code} {region} {region_name} {city} {zip} {latitude} {longitude} {timezone} {isp} {org} {autonomous_system} {country_flag}"
  • Possibly some non-rustic code there, feel free to school me.
  • serde_derive and service don't get really well (both export Serialize). Used an alias for one of them. Proper solution welcome.
  • Using a free API to get all the info. Only limitation this API has that we might sort of care about is that it's http, not https (on the free tier).
  • Includes country flag representation but it requires a font that includes them. See blocks doc for details.
    -Tested with IPv4 only.

@cfsmp3 cfsmp3 changed the title WIP: First attempt at reporting the external IP address Display external IP address information (NetworkManager) Dec 5, 2021
@MaxVerevkin
Copy link
Collaborator

Using a free API to get all the info. Only limitation this API has that we might sort of care about is that it's http, not https (on the free tier).

This should probably be mentioned in the docs.

@cfsmp3
Copy link
Contributor Author

cfsmp3 commented Dec 6, 2021

Using a free API to get all the info. Only limitation this API has that we might sort of care about is that it's http, not https (on the free tier).

This should probably be mentioned in the docs.

Added.

src/formatting.rs Outdated Show resolved Hide resolved
@MaxVerevkin
Copy link
Collaborator

This addition doesn't look NetworkManager-specific, so perhaps it could be added to net block instead? And just to point out, this functionality can be accomplished with custom block:

# County flag
[[block]]
block = "custom"
command = """curl -s http://ip-api.com/json | jq -r '.countryCode' | python -c 'print("".join(map(lambda x: chr(ord(x)+127397),input())))'"""
interval = 60

# County code
[[block]]
block = "custom"
command = """curl -s http://ip-api.com/json | jq -r '.countryCode'"""
interval = 60

# Address
[[block]]
block = "custom"
command = """curl -s http://ip-api.com/json | jq -r '.query'"""
interval = 60

@cfsmp3
Copy link
Contributor Author

cfsmp3 commented Dec 7, 2021

This addition doesn't look NetworkManager-specific, so perhaps it could be added to net block instead?

The net block doesn't have the very useful DBus listener stuff to detect changes as they happen. I actually started adding it there but it was clearly going in the wrong direction.

I could create a separate block just for this (which would be a copy of this one minus part of the preexisting stuff), but I'm not sure that's also a good idea.

And just to point out, this functionality can be accomplished with custom block:

First - I personally refuse to be spawning processing every X seconds to populate the bar, if I wanted that I would be using the old bar which has a trillion of those scripts :-) Second, with a custom block you won't be able to immediately react to changes anyway.

@cfsmp3 cfsmp3 changed the title Display external IP address information (NetworkManager) Create block to display external IP address information Dec 7, 2021
@cfsmp3
Copy link
Contributor Author

cfsmp3 commented Dec 7, 2021

I've built my own block with blackjack and hookers.

@cfsmp3 cfsmp3 force-pushed the external_ip_address branch 7 times, most recently from a522fb2 to 68edb86 Compare December 8, 2021 07:09
@cfsmp3 cfsmp3 force-pushed the external_ip_address branch from 68edb86 to 34d2e43 Compare December 9, 2021 17:15
@MaxVerevkin
Copy link
Collaborator

Only limitation this API has that we might sort of care about is that it's http, not https (on the free tier).

Why not use https://ipapi.co/json/ which is already used by weather block?

@cfsmp3
Copy link
Contributor Author

cfsmp3 commented Dec 12, 2021

Why not use https://ipapi.co/json/ which is already used by weather block?

Because I wasn't aware of it :-)

@cfsmp3
Copy link
Contributor Author

cfsmp3 commented Dec 12, 2021

@MaxVerevkin I've switched to that one. It's missing the ISP though, which is somewhat valuable, but I prefer having https over that specific field.

@MaxVerevkin
Copy link
Collaborator

Also I think NetworkManager thread should be optional (perhaps on by default?) because this block can be useful for non NetworkManager users too.

@cfsmp3
Copy link
Contributor Author

cfsmp3 commented Dec 13, 2021

Also I think NetworkManager thread should be optional (perhaps on by default?) because this block can be useful for non NetworkManager users too.

I've added that.
However, I do have a problem with the periodic updates: They aren't happening anywhere close to the configured interval. Can you take a look at the code, where am I going wrong? It seems quite straightforward, but with this config:

[[block]]
block = "external_ip"
format = "{ip} {country_flag}"
refresh_interval_success = 60

I'd expect one poll every minute, and that's not happening (I'm monitoring traffic with wireshark).

@MaxVerevkin
Copy link
Collaborator

It appears that postal can be null. Besides that, I don't know what could be wrong with your code - the update function runs correctly every N seconds. I don't have experience with linux network monitoring, but it seems to work just fine.

@cfsmp3
Copy link
Contributor Author

cfsmp3 commented Dec 13, 2021

  • the update function runs correctly every N seconds.

How do you know for sure?

@MaxVerevkin
Copy link
Collaborator

How do you know for sure?

I've put an eprintln! in there :)

@cfsmp3
Copy link
Contributor Author

cfsmp3 commented Dec 13, 2021

How do you know for sure?

I've put an eprintln! in there :)

OK, can't argue with that :-)

@MaxVerevkin
Copy link
Collaborator

There are clippy warnings, but apart from that, looks good to me.

@cfsmp3
Copy link
Contributor Author

cfsmp3 commented Dec 22, 2021

There are clippy warnings, but apart from that, looks good to me.

Fixed.

@MaxVerevkin
Copy link
Collaborator

Thanks! By the way, if you want to tinker with some async rust code, you can port this block overt to async version which will hopefully be merged at some point (#1369) :)

@MaxVerevkin MaxVerevkin merged commit 07f16e7 into greshake:master Dec 22, 2021
@cfsmp3 cfsmp3 deleted the external_ip_address branch December 22, 2021 15:42
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants