This repository has been archived by the owner on Sep 20, 2020. It is now read-only.
forked from hbons/SparkleShare
-
Notifications
You must be signed in to change notification settings - Fork 6
/
SparkleFetcherBase.cs
executable file
·220 lines (160 loc) · 7.28 KB
/
SparkleFetcherBase.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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
// SparkleShare, a collaboration and sharing tool.
// Copyright (C) 2010 Hylke Bons <hylkebons@gmail.com>
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.
using System;
using System.IO;
using System.Diagnostics;
using System.Text.RegularExpressions;
using System.Threading;
using Mono.Unix;
namespace SparkleLib {
// Sets up a fetcher that can get remote folders
public abstract class SparkleFetcherBase {
public delegate void StartedEventHandler ();
public delegate void FinishedEventHandler ();
public delegate void FailedEventHandler ();
public delegate void ProgressChangedEventHandler (double percentage);
public event StartedEventHandler Started;
public event FinishedEventHandler Finished;
public event FailedEventHandler Failed;
public event ProgressChangedEventHandler ProgressChanged;
protected string target_folder;
protected string remote_url;
private Thread thread;
public SparkleFetcherBase (string server, string remote_folder, string target_folder)
{
this.target_folder = target_folder;
this.remote_url = server + "/" + remote_folder;
}
public abstract bool Fetch ();
// Clones the remote repository
public void Start ()
{
SparkleHelpers.DebugInfo ("Fetcher", "[" + this.target_folder + "] Fetching folder: " + this.remote_url);
if (Started != null)
Started ();
if (Directory.Exists (this.target_folder))
Directory.Delete (this.target_folder, true);
string host = GetHost (this.remote_url);
if (String.IsNullOrEmpty (host)) {
if (Failed != null)
Failed ();
return;
}
DisableHostKeyCheckingForHost (host);
this.thread = new Thread (new ThreadStart (delegate {
if (Fetch ()) {
SparkleHelpers.DebugInfo ("Fetcher", "Finished");
EnableHostKeyCheckingForHost (host);
if (Finished != null)
Finished ();
} else {
SparkleHelpers.DebugInfo ("Fetcher", "Failed");
EnableHostKeyCheckingForHost (host);
if (Failed != null)
Failed ();
}
}));
this.thread.Start ();
}
public virtual void Stop ()
{
this.thread.Abort ();
this.thread.Join ();
}
public string RemoteUrl {
get {
return this.remote_url;
}
}
public void Dispose ()
{
if (this.thread != null) {
this.thread.Abort ();
this.thread.Join ();
}
}
protected void OnProgressChanged (double percentage) {
if (ProgressChanged != null)
ProgressChanged (percentage);
}
private void DisableHostKeyCheckingForHost (string host)
{
string path = SparkleConfig.DefaultConfig.HomePath;
if (!(SparkleBackend.Platform == PlatformID.Unix ||
SparkleBackend.Platform == PlatformID.MacOSX)) {
path = Environment.ExpandEnvironmentVariables ("%HOMEDRIVE%%HOMEPATH%");
}
string ssh_config_path = Path.Combine (path, ".ssh");
string ssh_config_file_path = SparkleHelpers.CombineMore (path, ".ssh", "config");
string ssh_config = Environment.NewLine + "Host " + host +
Environment.NewLine + "\tStrictHostKeyChecking no";
if (!Directory.Exists (ssh_config_path))
Directory.CreateDirectory (ssh_config_path);
if (File.Exists (ssh_config_file_path)) {
TextWriter writer = File.AppendText (ssh_config_file_path);
writer.WriteLine (ssh_config);
writer.Close ();
} else {
TextWriter writer = new StreamWriter (ssh_config_file_path);
writer.WriteLine (ssh_config);
writer.Close ();
}
UnixFileSystemInfo file_info = new UnixFileInfo (ssh_config_file_path);
file_info.FileAccessPermissions = (FileAccessPermissions.UserRead |
FileAccessPermissions.UserWrite);
SparkleHelpers.DebugInfo ("Fetcher", "Disabled host key checking");
}
private void EnableHostKeyCheckingForHost (string host)
{
string path = SparkleConfig.DefaultConfig.HomePath;
if (!(SparkleBackend.Platform == PlatformID.Unix ||
SparkleBackend.Platform == PlatformID.MacOSX)) {
path = Environment.ExpandEnvironmentVariables ("%HOMEDRIVE%%HOMEPATH%");
}
string ssh_config_file_path = SparkleHelpers.CombineMore (path, ".ssh", "config");
string ssh_config = Environment.NewLine + "Host " + host +
Environment.NewLine + "\tStrictHostKeyChecking no";
if (File.Exists (ssh_config_file_path)) {
StreamReader reader = new StreamReader (ssh_config_file_path);
string current_ssh_config = reader.ReadToEnd ();
reader.Close ();
current_ssh_config = current_ssh_config.Remove (
current_ssh_config.IndexOf (ssh_config), ssh_config.Length);
bool has_some_ssh_config = new Regex (@"[a-z]").IsMatch (current_ssh_config);
if (!has_some_ssh_config) {
File.Delete (ssh_config_file_path);
} else {
TextWriter writer = new StreamWriter (ssh_config_file_path);
writer.WriteLine (current_ssh_config);
writer.Close ();
UnixFileSystemInfo file_info = new UnixFileInfo (ssh_config_file_path);
file_info.FileAccessPermissions = (FileAccessPermissions.UserRead |
FileAccessPermissions.UserWrite);
}
}
SparkleHelpers.DebugInfo ("Fetcher", "Enabled host key checking");
}
private string GetHost (string url)
{
Regex regex = new Regex (@"(@|://)([a-z0-9\.-]+)(/|:)");
Match match = regex.Match (url);
if (match.Success)
return match.Groups [2].Value;
else
return null;
}
}
}