forked from NexerqDev/Verdant
-
Notifications
You must be signed in to change notification settings - Fork 0
/
LoginNew.xaml.cs
127 lines (112 loc) · 4.45 KB
/
LoginNew.xaml.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
namespace Verdant
{
/// <summary>
/// Interaction logic for LoginNew.xaml
/// </summary>
public partial class LoginNew : Window
{
private NaverAccount account;
public LoginNew(NaverAccount account)
{
InitializeComponent();
this.account = account;
// we specifically use this url redirect so we know when auth success
webBrowser.Navigate("https://nid.naver.com/nidlogin.login?mode=form&url=https://blog.naver.com/nexerq");
}
private void webBrowser_Navigating(object sender, System.Windows.Navigation.NavigatingCancelEventArgs e)
{
if (e.Uri.AbsoluteUri == "https://blog.naver.com/nexerq")
{
// check if auth really success
e.Cancel = true;
try
{
getSystemCookiesAndTest();
Close();
}
catch
{
MessageBox.Show("Something went very wrong while logging in...", "Verdant");
Close();
}
}
}
private void getSystemCookiesAndTest()
{
string cookies = Tools.GetCookieStringData("https://naver.com");
if (cookies != null && cookies.Contains("NID_AUT"))
{
// success - now we add properly, then double check and go!
foreach (string s in cookies.Split(new string[] { "; " }, StringSplitOptions.None))
{
string[] ss = s.Split('=');
addNaverCookieData(ss[0], ss[1]);
}
Task.Run(() => account.EnsureLoggedIn()).Wait();
account.SaveCookies();
account.LoggedIn = true;
}
}
private void resetButton_Click(object sender, RoutedEventArgs e)
{
webBrowser.Navigate("https://nid.naver.com/nidlogin.login?mode=form&url=https://blog.naver.com/nexerq");
}
private void addNaverCookieData(string name, string value)
{
Cookie c = new Cookie();
c.Name = name;
c.Value = value;
c.Domain = ".naver.com";
c.Path = "/";
c.Expires = DateTime.Now.AddMonths(3);
c.HttpOnly = (c.Name == "NID_AUT") ? true : false;
account.Cookies.Add(c);
}
Regex devToolsRegex = new Regex("^(.*?)\\t(.*?)\\t", RegexOptions.Multiline);
private async void tryClipboardLogin()
{
// clipboard cookie based login - fuck naver's absolutely terrible captcha, and cbs working out how bvsd works! :)
// we have to use dev tools copy paste though because NID_AUT is httponly, so cant just use document.cookie :(
try
{
string clipboard = Clipboard.GetText(TextDataFormat.Text);
if (!clipboard.Contains("NID_AUT\t"))
return;
var mbr = MessageBox.Show("Found what looks like Chrome devtools cookies clipboard data to login with. Use it?", "Verdant", MessageBoxButton.YesNo, MessageBoxImage.Question);
if (mbr == MessageBoxResult.No)
return;
webBrowser.IsEnabled = false;
foreach (Match m in devToolsRegex.Matches(clipboard))
addNaverCookieData(m.Groups[1].Value, m.Groups[2].Value);
// catch will catch this too
await account.EnsureLoggedIn();
account.SaveCookies();
Close();
}
catch
{
MessageBox.Show("Clipboard login failed.", "Verdant", MessageBoxButton.OK, MessageBoxImage.Asterisk);
webBrowser.IsEnabled = true;
}
}
private void Window_Loaded(object sender, RoutedEventArgs e)
{
tryClipboardLogin();
}
}
}