Skip to content

Commit 2e0e540

Browse files
committed
webui: Automatically check whether IPv6 is supported
1 parent b547030 commit 2e0e540

File tree

2 files changed

+19
-2
lines changed

2 files changed

+19
-2
lines changed

plugin/smartdns-ui/src/http_server.rs

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,8 @@ cfg_if::cfg_if! {
6262
const HTTP_SERVER_DEFAULT_PASSWORD: &str = "password";
6363
const HTTP_SERVER_DEFAULT_USERNAME: &str = "admin";
6464
const HTTP_SERVER_DEFAULT_WWW_ROOT: &str = "/usr/share/smartdns/wwwroot";
65-
const HTTP_SERVER_DEFAULT_IP: &str = "http://[::]:6080";
65+
const HTTP_SERVER_DEFAULT_IPV6: &str = "http://[::]:6080";
66+
const HTTP_SERVER_DEFAULT_IP: &str = "http://0.0.0.0:6080";
6667

6768
#[derive(Clone)]
6869
pub struct HttpServerConfig {
@@ -77,8 +78,15 @@ pub struct HttpServerConfig {
7778

7879
impl HttpServerConfig {
7980
pub fn new() -> Self {
81+
82+
let host_ip = if utils::is_ipv6_supported() {
83+
HTTP_SERVER_DEFAULT_IPV6.to_string()
84+
} else {
85+
HTTP_SERVER_DEFAULT_IP.to_string()
86+
};
87+
8088
HttpServerConfig {
81-
http_ip: HTTP_SERVER_DEFAULT_IP.to_string(),
89+
http_ip: host_ip,
8290
http_root: HTTP_SERVER_DEFAULT_WWW_ROOT.to_string(),
8391
username: HTTP_SERVER_DEFAULT_USERNAME.to_string(),
8492
password: utils::hash_password(HTTP_SERVER_DEFAULT_PASSWORD, Some(1000)).unwrap(),

plugin/smartdns-ui/src/utils.rs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,3 +97,12 @@ pub fn is_dir_writable(path: &str) -> bool {
9797
let path = std::ffi::CString::new(path).unwrap();
9898
unsafe { libc::access(path.as_ptr(), libc::W_OK) == 0 }
9999
}
100+
101+
pub fn is_ipv6_supported() -> bool {
102+
let sock = unsafe { libc::socket(libc::AF_INET6, libc::SOCK_STREAM, 0) };
103+
if sock < 0 {
104+
return false;
105+
}
106+
unsafe { libc::close(sock) };
107+
true
108+
}

0 commit comments

Comments
 (0)