├── .gitignore ├── Cargo.toml ├── LICENSE ├── README.md └── src ├── lib.rs └── main.rs /.gitignore: -------------------------------------------------------------------------------- 1 | /target 2 | **/*.rs.bk 3 | -------------------------------------------------------------------------------- /Cargo.toml: -------------------------------------------------------------------------------- 1 | [package] 2 | name = "local_ipaddress" 3 | version = "0.1.3" 4 | description = "Get your local IP address without panic" 5 | authors = ["egmkang wang "] 6 | edition = "2018" 7 | license = "MIT" 8 | 9 | readme = "README.md" 10 | repository = "https://github.com/egmkang/local_ipaddress" 11 | homepage = "https://github.com/egmkang/local_ipaddress" 12 | document = "https://docs.rs/local_ipaddress" 13 | 14 | # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html 15 | 16 | [dependencies] 17 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2019 egmkang wang 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # local_ipaddress 2 | 3 | Get your local ip address in Rust, using `UdpSocket` to get local ip address(not `network interface` or `ifconfig`), and won't `panic`. 4 | 5 | API Docs: [https://docs.rs/local_ipaddress](https://docs.rs/local_ipaddress) 6 | 7 | ### Usage 8 | 9 | Add this to your Cargo.toml: 10 | 11 | 12 | ``` 13 | [dependencies] 14 | local_ipaddress = "0.1.3" 15 | ``` 16 | 17 | ### Getting Started 18 | 19 | ```rust 20 | use local_ipaddress; 21 | 22 | fn main() { 23 | println!("{}", local_ipaddress::get().unwrap()); 24 | } 25 | ``` 26 | 27 | It works fine with both `Windows` and `Linux`. 28 | 29 | ### License 30 | 31 | MIT 32 | -------------------------------------------------------------------------------- /src/lib.rs: -------------------------------------------------------------------------------- 1 | //! A crate for get local ip address, 2 | //! without using `ifconfig` or scanning `network interface` 3 | 4 | use std::net::UdpSocket; 5 | 6 | /// get the local ip address, return an `Option`. when it fail, return `None`. 7 | pub fn get() -> Option { 8 | let socket = match UdpSocket::bind("0.0.0.0:0") { 9 | Ok(s) => s, 10 | Err(_) => return None, 11 | }; 12 | 13 | match socket.connect("8.8.8.8:80") { 14 | Ok(()) => (), 15 | Err(_) => return None, 16 | }; 17 | 18 | match socket.local_addr() { 19 | Ok(addr) => return Some(addr.ip().to_string()), 20 | Err(_) => return None, 21 | }; 22 | } 23 | -------------------------------------------------------------------------------- /src/main.rs: -------------------------------------------------------------------------------- 1 | extern crate local_ipaddress; 2 | 3 | fn main() { 4 | println!("{}", local_ipaddress::get().unwrap()); 5 | } 6 | --------------------------------------------------------------------------------