Skip to content

Instantly share code, notes, and snippets.

@zhongfly
Last active February 3, 2024 07:03
Show Gist options
  • Save zhongfly/2a4e47b844edc22d0cc816f751fe9128 to your computer and use it in GitHub Desktop.
Save zhongfly/2a4e47b844edc22d0cc816f751fe9128 to your computer and use it in GitHub Desktop.
Get windows system proxy(only support http proxy) and auto set proxy for mpv and ytdl_hook
local mp = require 'mp'
function string:trim()
return (self:gsub("^%s*(.-)%s*$", "%1"))
end
local function pwshexec(command)
local args = {
"powershell", "-NoProfile", "-Command"
}
table.insert(args,command)
mp.msg.debug("Running: " .. table.concat(args, " "))
local ret = mp.command_native({name = "subprocess",
args = args,
playback_only = false,
capture_stdout = true,
capture_stderr = true})
if ret.killed_by_us or (ret.status ~= 0) or ret.stdout=="" then
return ""
else
return (ret.stdout:gsub("\r\n$", "")):trim()
end
end
local function get_proxy()
local ProxyEnable = pwshexec([=[(Get-ItemProperty -Path 'Registry::HKCU\Software\Microsoft\Windows\CurrentVersion\Internet Settings').ProxyEnable -eq 1]=])
mp.msg.debug(ProxyEnable)
if ProxyEnable == "True" then
local ProxyServer = pwshexec([=[((Get-ItemProperty -Path 'Registry::HKCU\Software\Microsoft\Windows\CurrentVersion\Internet Settings').ProxyServer | Select-String -Pattern '(\w+\.?)*:\d+').Matches[0].Value]=])
if ProxyServer~="" then
ProxyServer = "http://" .. ProxyServer
return ProxyServer
end
end
end
local proxy = get_proxy()
mp.msg.debug(proxy)
if proxy then
if mp.get_property("http-proxy")=="" then
mp.set_property("http-proxy",proxy)
mp.msg.info("Auto set http-proxy:" .. mp.get_property("http-proxy"))
end
local raw_options = mp.get_property_native("options/ytdl-raw-options")
if raw_options["proxy"]==nil or raw_options["proxy"] == ""then
raw_options["proxy"] = proxy
mp.set_property_native("options/ytdl-raw-options",raw_options)
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment