导航

C#获取arukas 樱花免费docker容器IP地址和端口

Posted on 2019-08-16 14:45  无名炮灰  阅读(569)  评论(0)    收藏  举报

arukas 樱花免费docker容器,可以安装linux系统,但是每隔一段时间会重启,重启以后IP地址和映射到公网的端口都会变,获取IP和端口,我研究了很久终于找到了C#获取IP和端口的办法,用来搭建梯子很不错哦。

//Framework版本必须是4.5以上的
ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12;//必须要,不然无法访问https

string urlAddr = @"https://app.arukas.io/api/services/……appid……";

HttpWebRequest myReq = (HttpWebRequest)HttpWebRequest.Create(urlAddr);
myReq.Method = "GET";
myReq.ContentType = "application/json";
string username = "……Token……";
string password = "……Secret……";
//注意这里的格式哦,为 "username:password"
string usernamePassword = username + ":" + password;
string code = Convert.ToBase64String(Encoding.ASCII.GetBytes(usernamePassword));
myReq.Headers.Add("Authorization", "Basic " + code);

HttpWebResponse response = (HttpWebResponse)myReq.GetResponse();
StreamReader reader = new StreamReader(response.GetResponseStream(), Encoding.UTF8);
string content = reader.ReadToEnd();
JObject MyData = JsonConvert.DeserializeObject<JObject>(content);
object resultStr = MyData["data"]["attributes"]["port-mappings"][0][0]["host"];
Regex reg = new Regex(@"\d{1,3}-\d{1,3}-\d{1,3}-\d{1,3}");
Match mc = reg.Match(resultStr.ToString());
string ipStr = mc.ToString().Replace('-', '.');
string oldPort = MyData["data"]["attributes"]["port-mappings"][0][0]["container-port"].ToString();
string newPort = MyData["data"]["attributes"]["port-mappings"][0][0]["service-port"].ToString();
HttpContext.Current.Response.Write(ipStr + ":" + newPort + "(" + oldPort + ")");