I'm trying to get the IPv4 from the URL below:
use url::{Url, Host};
use std::net::{SocketAddr, IpAddr};
fn main () {
let url = Url::parse("rtsp://admin:12345#192.168.1.178:10554/tcp/av0_0").unwrap();
let port = url.port().unwrap_or(554);
let ip_address = match url.host() {
Some(Host::Ipv4(ipv4)) => SocketAddr::new(IpAddr::V4(ipv4), port),
Some(Host::Ipv6(ipv6)) => SocketAddr::new(IpAddr::V6(ipv6), port),
None => panic!("missing host"),
_ => panic!(format!("only IP hosts are accepted. Your host: {:?}", url.host()))
};
println!("{}", ip_address);
}
Playground
but it panics:
thread 'main' panicked at 'only IP hosts are accepted. Your host: Some(Domain("192.168.1.178"))', src/main.rs:11:14
What is wrong? The host is clearly an ip address.
Looking a the bug tracker, I found IP is not recognized in RTSP url.
The answer is apparently that url follows / implements the URL Standard (which makes sense as it was developed in the context of Servo), and the URL Standard only requires IPv4 address recognition of a select number of schemes because those schemes (apparently) handle IPv4 addresses specially: https://github.com/servo/rust-url/issues/577
Specifically: the special schemes are ftp, http, https, ws, wss (there's also file but it's its own category), and the differences in host representation are that:
a special scheme's host can be ipv4, ipv6, or domain
a non-special scheme's host can be ipv6, opaque, empty, or null
url will in fact parse ipv6 for non-special schemes, though everything else it just dumps into Some(Domain(...)) (or None).
Anyway this means that if you get a Domain result, you probably want to try and parse it as an IP.
Incidentally, you don't have to format! to format your panic messages, panic! will do that internally.
Related
When I store IP of client using req.ip and req.remote_ip then it showing different result when request twice using same device and internet connection.
for first req it returns:-
2402:8100:3092:16f4:1:1:cd28:307f
for second request it returns
27.97.134.92
which is ipv4.
Is there any way to get either only ipv6 or only ipv4 address?
The new NBIOT demo modules from O2 - we are testing - they only accept an IP address as a broker host rather than URL [mqtt.googleapis.com]. If i run DNS lookup this is fine - but how stable is the IP address associated with the mqtt.googleapis.com ??
I have the DNS lookup here 74.125.201.206
How long will it remain stable / the same ??
stream {
upstream google_mqtt {
server mqtt.googleapis.com:8883;
}
server {
listen 8883;
proxy_pass google_mqtt;
}
}
Instead of the mqtt url i want to insert IP address
Why would you want to hard code the IP address? You are just setting yourself up for it to fail at the moment you can't fix it (e.g. while on vacation)
You shouldn't assume an IP address returned by a DNS query is good for any longer than the TTL value returned with the response.
Hostnames are a deliberate abstraction so you don't have to worry about if the IP address changes, be it due to a failure, maintenance, load balancing.
Just DON'T hardcode the IP address.
If the module you mentioned REALLY only accepts IP addresses then you need to raise a bug against the supplier saying this needs fixing, especially as this is for a field deployed device that you probably can't easily update once deployed.
Is there a way to use the whois gem (https://github.com/weppos/whois) with a -h flag? I am looking to run whois queries against a specific database host in my rails project (e.g. whois -h whois.myserver.example google.com)
If not, are there other ways to accomplish this?
From the documentation at https://www.rubydoc.info/gems/whois/Whois/Client
Parameters:
settings (Hash) (defaults to: {}) —
Hash of settings to customize the client behavior.
Options Hash (settings):
:timeout (Integer, nil) — default: DEFAULT_TIMEOUT —
The timeout for a WHOIS query, expressed in seconds.
:bind_host (String) — default: nil —
Providing an IP address or hostname will bind the Socket connection to the specific local host.
:bind_port (Fixnum) — default: nil —
Providing port number will bind the Socket connection to the specific local port.
:host (String, nil) — default: nil —
The server host to query. Leave it blank for intelligent detection.
So
client = Whois::Client.new(:host => "whois.myserver.example")
should be what you need.
No idea if you need to change the port too.
I used symfony 1.4 to create my application.
I'd like to get the IP adress of the current server to put it within soap request
So, how can i get the IP address of the current server?
For most situations, using $_SERVER['SERVER_ADDR']; will work. If that doesn't work you can try $ip = gethostbyname(gethostname());
If you have access to the $request object and it is a sfWebRequest (typical request from a browser) you can use:
$request->getPathInfoArray()['SERVER_ADDR']
Premise of the following method: your domain name has only one IP resolution
Using PHP:
gethostbyname($_SERVER['SERVER_NAME'])
$_SERVER['SERVER_NAME']will generally return your domain name (server_name / ServerName is configured in Nginx / Apache server), and then use gethostbyname().
About $_SERVER['SERVER_ADDR'], it often return a LAN IP address (I only have one server, one domain name, no reverse proxy; cloud server).
About gethostname()
In the test, it returns the name of the server (host name, not the domain name you use), and then uses gethostbyname(), will return a LAN IP.
More can be used https://checkip.amazonaws.com/ Get the current IP.
By default, example.com resolve to 123.123.123.123,
But If I want it to be resolved to 100.100.100.100.
For http, I can simply change the url to http://100.100.100.100 with a header "Host: example.com".
But it's not working for HTTPS.(Error: SSL certificate problem: Invalid certificate chain).
My question is not why, and I do not want to skip the certificate validation.
How can I get the same effect in Objective-C like curl's
--resolve option:
--resolve <host:port:address>
Provide a custom address for a specific host and port pair. Using this, you can make the curl requests(s)
use a specified address and prevent the otherwise normally resolved address to be used. Consider it a sort
of /etc/hosts alternative provided on the command line. The port number should be the number used for the
specific protocol the host will be used for. It means you need several entries if you want to provide
address for the same host but different ports.
In other words, How to make custom DNS query in HTTPS requests in Objective-C?
When you are using https, the address that you use in your request, and the address given to you by the certificate returned by the server, must agree.
If you send a request to https://100.100.100.100 then the server must return a certificate for 100.100.100.100. Even if you connected successfully to https:// www.xyz.com, and www.xyz.com resolved to 100.100.100.100, connecting to https://100.100.100.100 isn't going to work, cannot work, and absolutely must not work, because the server will return a certificate for www.xyz.com and not for 100.100.100.100.
I see following options:
Use your own DNS server with corresponding configuration of host/ip entry
If you want to stick with Objective C, there is a guideline frome apple Overriding SSL Chain Validation Correctly
Use libcurl which supports the feature you mentioned: http://curl.haxx.se/libcurl/c/resolve.html
example
#include <stdio.h>
#include <curl/curl.h>
int main(void)
{
CURL *curl;
CURLcode res = CURLE_OK;
struct curl_slist *host = NULL;
/* Each single name resolve string should be written using the format
HOST:PORT:ADDRESS where HOST is the name libcurl will try to resolve,
PORT is the port number of the service where libcurl wants to connect to
the HOST and ADDRESS is the numerical IP address
*/
host = curl_slist_append(NULL, "example.com:80:127.0.0.1");
curl = curl_easy_init();
if(curl) {
curl_easy_setopt(curl, CURLOPT_RESOLVE, host);
curl_easy_setopt(curl, CURLOPT_URL, "http://example.com");
res = curl_easy_perform(curl);
/* always cleanup */
curl_easy_cleanup(curl);
}
curl_slist_free_all(host);
return (int)res;
}
Update:
Since author don't want to skip certificate validation this is not an option now:
You can try to ignore ssl certificate in AFNetworking in your case
I want to allow invalid SSL certificates with AFNetworking