1+ using System ;
2+ using System . Collections . Generic ;
3+ using System . Diagnostics ;
4+ using System . IO ;
5+ using System . Linq ;
6+ using System . Net ;
7+ using System . Security . Cryptography ;
8+ using System . Text ;
9+ using System . Text . Json ;
10+
11+ namespace ryu_s . BrowserCookie
12+ {
13+ public class BuildinManager : IBrowserManager
14+ {
15+ public BrowserType Type => BrowserType . Buildin ; // 新しいBrowserTypeが必要
16+
17+ public List < IBrowserProfile > GetProfiles ( )
18+ {
19+ var profiles = new List < IBrowserProfile > ( ) ;
20+ profiles . Add ( new BuildinProfile ( "default" ) ) ;
21+ return profiles ;
22+ }
23+ }
24+
25+ public class BuildinProfile : IBrowserProfile
26+ {
27+ public string Path { get ; }
28+ public string ProfileName { get ; }
29+ public BrowserType Type => BrowserType . Buildin ;
30+
31+ public BuildinProfile ( string profileName )
32+ {
33+ ProfileName = profileName ?? "default" ;
34+ Path = GetCookieFilePath ( ProfileName ) ;
35+ }
36+
37+ public Cookie GetCookie ( string domain , string name )
38+ {
39+ var cookies = GetCookieCollection ( domain ) ;
40+ return cookies . FirstOrDefault ( c => c . Name . Equals ( name , StringComparison . OrdinalIgnoreCase ) ) ;
41+ }
42+
43+ public List < Cookie > GetCookieCollection ( string domain )
44+ {
45+
46+ Console . WriteLine ( $ "Loading cookies for domain: { domain } from { Path } ") ;
47+
48+ string fileName = domain switch
49+ {
50+ "mirrativ.com" => "Mirrativ.bin" ,
51+ "www.mirrativ.com" => "Mirrativ.bin" ,
52+ "youtube.com" => "YouTubeLive.bin" ,
53+ null => "unknown" ,
54+ _ => "unknown" // これがあると安全です
55+ } ;
56+
57+ var allCookies = LoadAllCookies ( fileName ) ;
58+ var result = new List < Cookie > ( ) ;
59+
60+ foreach ( var cookie in allCookies ) {
61+ // ドメインマッチングロジック
62+ if ( IsHostMatch ( cookie . Domain , domain ) )
63+ {
64+ result . Add ( cookie ) ;
65+ }
66+ Debug . WriteLine ( cookie . Domain ) ;
67+ }
68+ return result ;
69+ }
70+
71+ private List < Cookie > LoadAllCookies ( string file )
72+ {
73+ var cookieList = new List < Cookie > ( ) ;
74+
75+ var path1 = System . IO . Path . Combine ( Path , file ) ;
76+
77+ if ( ! File . Exists ( path1 ) )
78+ {
79+ return cookieList ;
80+ }
81+
82+ try
83+ {
84+ // 暗号化されたファイルを読み込み
85+ var encrypted = File . ReadAllBytes ( path1 ) ;
86+
87+ // DPAPI で復号
88+ var bytes = ProtectedData . Unprotect ( encrypted , optionalEntropy : null , scope : DataProtectionScope . LocalMachine ) ;
89+ var json = Encoding . UTF8 . GetString ( bytes ) ;
90+
91+ // JSON を CookieDto のリストにデシリアライズ
92+ var cookieDtos = JsonSerializer . Deserialize < List < CookieDto > > ( json ) ;
93+
94+ if ( cookieDtos != null )
95+ {
96+ foreach ( var dto in cookieDtos )
97+ {
98+ try
99+ {
100+ var cookie = new Cookie ( dto . Name , dto . Value , dto . Path ?? "/" , dto . Domain ?? "" ) ;
101+
102+ // 有効期限を設定
103+ if ( dto . Expires . HasValue )
104+ {
105+ cookie . Expires = dto . Expires . Value ;
106+ }
107+
108+ // その他のプロパティを設定
109+ cookie . HttpOnly = dto . IsHttpOnly ;
110+ cookie . Secure = dto . IsSecure ;
111+
112+ cookieList . Add ( cookie ) ;
113+ }
114+ catch ( Exception )
115+ {
116+ // 無効なクッキーは無視して続行
117+ }
118+ }
119+ }
120+ }
121+ catch ( Exception )
122+ {
123+ // 復号化エラーまたはJSONパースエラーの場合は空のリストを返す
124+ }
125+
126+ return cookieList ;
127+ }
128+
129+ private bool IsHostMatch ( string cookieDomain , string requestDomain )
130+ {
131+ if ( string . IsNullOrEmpty ( cookieDomain ) || string . IsNullOrEmpty ( requestDomain ) )
132+ {
133+ return false ;
134+ }
135+
136+ // 完全一致
137+ if ( cookieDomain . Equals ( requestDomain , StringComparison . OrdinalIgnoreCase ) )
138+ {
139+ return true ;
140+ }
141+
142+ // ドット付きドメイン(.example.com)の場合、サブドメインマッチング
143+ if ( cookieDomain . StartsWith ( "." ) )
144+ {
145+ var domain = cookieDomain . Substring ( 1 ) ;
146+ return requestDomain . Equals ( domain , StringComparison . OrdinalIgnoreCase ) ||
147+ requestDomain . EndsWith ( "." + domain , StringComparison . OrdinalIgnoreCase ) ;
148+ }
149+
150+ // 逆パターン:requestDomainがサブドメインの場合
151+ if ( requestDomain . Contains ( cookieDomain ) )
152+ {
153+ return requestDomain . EndsWith ( "." + cookieDomain , StringComparison . OrdinalIgnoreCase ) ||
154+ requestDomain . Equals ( cookieDomain , StringComparison . OrdinalIgnoreCase ) ;
155+ }
156+
157+ if ( ! cookieDomain . StartsWith ( "." ) && cookieDomain . EndsWith ( requestDomain ) )
158+ {
159+ return true ;
160+ }
161+
162+ return false ;
163+ }
164+
165+ private string GetCookieFilePath ( string siteName )
166+ {
167+ var exeDir = System . IO . Path . GetDirectoryName ( System . Reflection . Assembly . GetExecutingAssembly ( ) . Location )
168+ ?? AppDomain . CurrentDomain . BaseDirectory ;
169+ var folder = System . IO . Path . Combine ( exeDir , "Cookies" ) ;
170+ Directory . CreateDirectory ( folder ) ;
171+ return folder ;
172+ }
173+ }
174+
175+ // CookieDto クラス(CookieStorage.cs から移動または重複定義)
176+ public class CookieDto
177+ {
178+ public string Name { get ; set ; }
179+ public string Value { get ; set ; }
180+ public string Domain { get ; set ; }
181+ public string Path { get ; set ; }
182+ public DateTime ? Expires { get ; set ; } // null = session cookie
183+ public bool IsHttpOnly { get ; set ; }
184+ public bool IsSecure { get ; set ; }
185+ }
186+ }
0 commit comments