2016-01-16 04:21:36 +01:00
|
|
|
|
/*
|
|
|
|
|
|
_ _ _ ____ _ _____
|
|
|
|
|
|
/ \ _ __ ___ | |__ (_)/ ___| | |_ ___ __ _ _ __ ___ | ___|__ _ _ __ _ __ ___
|
|
|
|
|
|
/ _ \ | '__|/ __|| '_ \ | |\___ \ | __|/ _ \ / _` || '_ ` _ \ | |_ / _` || '__|| '_ ` _ \
|
|
|
|
|
|
/ ___ \ | | | (__ | | | || | ___) || |_| __/| (_| || | | | | || _|| (_| || | | | | | | |
|
|
|
|
|
|
/_/ \_\|_| \___||_| |_||_||____/ \__|\___| \__,_||_| |_| |_||_| \__,_||_| |_| |_| |_|
|
|
|
|
|
|
|
2017-01-02 20:05:21 +01:00
|
|
|
|
Copyright 2015-2017 Łukasz "JustArchi" Domeradzki
|
2016-01-16 04:21:36 +01:00
|
|
|
|
Contact: JustArchi@JustArchi.net
|
|
|
|
|
|
|
|
|
|
|
|
Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
|
|
you may not use this file except in compliance with the License.
|
|
|
|
|
|
You may obtain a copy of the License at
|
|
|
|
|
|
|
|
|
|
|
|
http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
|
|
|
|
|
|
|
|
Unless required by applicable law or agreed to in writing, software
|
|
|
|
|
|
distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
|
|
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
|
|
See the License for the specific language governing permissions and
|
|
|
|
|
|
limitations under the License.
|
|
|
|
|
|
|
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
|
|
using System;
|
2016-03-10 01:20:17 +01:00
|
|
|
|
using System.Linq;
|
2016-01-03 20:36:13 +01:00
|
|
|
|
using System.ServiceModel;
|
2016-01-03 22:23:30 +01:00
|
|
|
|
using System.ServiceModel.Channels;
|
2017-01-06 15:32:12 +01:00
|
|
|
|
using ArchiSteamFarm.Localization;
|
2016-01-03 20:36:13 +01:00
|
|
|
|
|
|
|
|
|
|
namespace ArchiSteamFarm {
|
|
|
|
|
|
[ServiceContract]
|
|
|
|
|
|
internal interface IWCF {
|
2016-08-01 18:17:51 +02:00
|
|
|
|
[OperationContract]
|
|
|
|
|
|
string GetStatus();
|
|
|
|
|
|
|
2016-01-03 20:36:13 +01:00
|
|
|
|
[OperationContract]
|
2016-07-30 12:17:01 +08:00
|
|
|
|
string HandleCommand(string input);
|
2016-01-03 20:36:13 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
2016-07-08 07:41:36 +02:00
|
|
|
|
internal sealed class WCF : IWCF, IDisposable {
|
2016-10-27 22:51:44 +02:00
|
|
|
|
internal bool IsServerRunning => ServiceHost != null;
|
|
|
|
|
|
|
2016-11-24 07:32:16 +01:00
|
|
|
|
private Client Client;
|
|
|
|
|
|
private ServiceHost ServiceHost;
|
2016-03-22 17:26:39 +01:00
|
|
|
|
|
2016-11-24 07:32:16 +01:00
|
|
|
|
public void Dispose() {
|
|
|
|
|
|
StopClient();
|
|
|
|
|
|
StopServer();
|
2016-03-06 23:28:56 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
2016-11-24 07:32:16 +01:00
|
|
|
|
public string GetStatus() => Program.GlobalConfig.SteamOwnerID == 0 ? "{}" : Bot.GetAPIStatus();
|
|
|
|
|
|
|
2016-07-30 12:17:01 +08:00
|
|
|
|
public string HandleCommand(string input) {
|
2016-07-08 07:41:36 +02:00
|
|
|
|
if (string.IsNullOrEmpty(input)) {
|
2017-01-31 01:10:01 +01:00
|
|
|
|
ASF.ArchiLogger.LogNullError(nameof(input));
|
2016-07-08 07:41:36 +02:00
|
|
|
|
return null;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2016-08-01 18:17:51 +02:00
|
|
|
|
if (Program.GlobalConfig.SteamOwnerID == 0) {
|
2017-01-06 15:32:12 +01:00
|
|
|
|
return Strings.ErrorWCFAccessDenied;
|
2016-08-01 18:17:51 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
2016-07-08 07:41:36 +02:00
|
|
|
|
Bot bot = Bot.Bots.Values.FirstOrDefault();
|
|
|
|
|
|
if (bot == null) {
|
2017-01-06 15:32:12 +01:00
|
|
|
|
return Strings.ErrorNoBotsDefined;
|
2016-07-08 07:41:36 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
string command = "!" + input;
|
2016-12-11 18:16:55 +01:00
|
|
|
|
|
|
|
|
|
|
// TODO: This should be asynchronous, but for some reason Mono doesn't return any WCF output if it is
|
|
|
|
|
|
// We must keep it synchronous until either Mono gets fixed, or culprit for freeze located (and corrected)
|
|
|
|
|
|
string output = bot.Response(Program.GlobalConfig.SteamOwnerID, command).Result;
|
2016-07-08 07:41:36 +02:00
|
|
|
|
|
2017-01-31 01:10:01 +01:00
|
|
|
|
ASF.ArchiLogger.LogGenericInfo(string.Format(Strings.WCFAnswered, input, output));
|
2016-07-08 07:41:36 +02:00
|
|
|
|
return output;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2016-11-24 07:32:16 +01:00
|
|
|
|
internal static void Init() {
|
2016-12-11 16:56:33 +01:00
|
|
|
|
if (string.IsNullOrEmpty(Program.GlobalConfig.WCFHost)) {
|
|
|
|
|
|
Program.GlobalConfig.WCFHost = Program.GetUserInput(ASF.EUserInputType.WCFHostname);
|
2016-11-24 07:32:16 +01:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
internal string SendCommand(string input) {
|
|
|
|
|
|
if (string.IsNullOrEmpty(input)) {
|
2017-01-31 01:10:01 +01:00
|
|
|
|
ASF.ArchiLogger.LogNullError(nameof(input));
|
2016-11-24 07:32:16 +01:00
|
|
|
|
return null;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2017-02-03 09:54:09 +01:00
|
|
|
|
Binding binding = GetTargetBinding();
|
|
|
|
|
|
if (binding == null) {
|
|
|
|
|
|
ASF.ArchiLogger.LogNullError(nameof(binding));
|
|
|
|
|
|
return null;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
string url = GetUrlFromBinding(binding);
|
|
|
|
|
|
if (string.IsNullOrEmpty(url)) {
|
|
|
|
|
|
ASF.ArchiLogger.LogNullError(nameof(url));
|
|
|
|
|
|
return null;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
ASF.ArchiLogger.LogGenericInfo(string.Format(Strings.WCFSendingCommand, input, url));
|
2016-12-16 15:02:41 +01:00
|
|
|
|
|
2016-11-24 07:32:16 +01:00
|
|
|
|
if (Client == null) {
|
2016-12-26 15:59:47 +01:00
|
|
|
|
Client = new Client(
|
2017-02-03 09:54:09 +01:00
|
|
|
|
binding,
|
|
|
|
|
|
new EndpointAddress(url)
|
2016-12-26 15:59:47 +01:00
|
|
|
|
);
|
2016-11-24 07:32:16 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
return Client.HandleCommand(input);
|
2016-07-08 07:41:36 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
2016-01-03 20:36:13 +01:00
|
|
|
|
internal void StartServer() {
|
2016-11-13 15:51:35 +01:00
|
|
|
|
if (IsServerRunning) {
|
2016-01-03 20:36:13 +01:00
|
|
|
|
return;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2017-02-03 09:54:09 +01:00
|
|
|
|
Binding binding = GetTargetBinding();
|
|
|
|
|
|
if (binding == null) {
|
|
|
|
|
|
ASF.ArchiLogger.LogNullError(nameof(binding));
|
|
|
|
|
|
return;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
string url = GetUrlFromBinding(binding);
|
|
|
|
|
|
if (string.IsNullOrEmpty(url)) {
|
|
|
|
|
|
ASF.ArchiLogger.LogNullError(nameof(url));
|
|
|
|
|
|
return;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
ASF.ArchiLogger.LogGenericInfo(string.Format(Strings.WCFStarting, url));
|
2016-01-03 20:36:13 +01:00
|
|
|
|
|
|
|
|
|
|
try {
|
2017-02-03 09:54:09 +01:00
|
|
|
|
ServiceHost = new ServiceHost(typeof(WCF), new Uri(url));
|
2016-12-26 15:59:47 +01:00
|
|
|
|
ServiceHost.AddServiceEndpoint(
|
|
|
|
|
|
typeof(IWCF),
|
2017-02-03 09:54:09 +01:00
|
|
|
|
binding,
|
2016-12-26 15:59:47 +01:00
|
|
|
|
string.Empty
|
|
|
|
|
|
);
|
2017-02-03 09:54:09 +01:00
|
|
|
|
|
2016-01-03 20:36:13 +01:00
|
|
|
|
ServiceHost.Open();
|
2016-12-11 17:58:53 +01:00
|
|
|
|
|
2017-01-31 01:10:01 +01:00
|
|
|
|
ASF.ArchiLogger.LogGenericInfo(Strings.WCFReady);
|
2016-11-15 15:43:34 +01:00
|
|
|
|
} catch (AddressAccessDeniedException) {
|
2017-01-31 01:10:01 +01:00
|
|
|
|
ASF.ArchiLogger.LogGenericError(Strings.ErrorWCFAddressAccessDeniedException);
|
2016-01-03 20:36:13 +01:00
|
|
|
|
} catch (Exception e) {
|
2017-01-31 01:10:01 +01:00
|
|
|
|
ASF.ArchiLogger.LogGenericException(e);
|
2016-01-03 20:36:13 +01:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
internal void StopServer() {
|
2016-11-13 15:51:35 +01:00
|
|
|
|
if (!IsServerRunning) {
|
2016-01-03 20:36:13 +01:00
|
|
|
|
return;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2016-07-28 18:27:01 +02:00
|
|
|
|
if (ServiceHost.State != CommunicationState.Closed) {
|
|
|
|
|
|
try {
|
|
|
|
|
|
ServiceHost.Close();
|
|
|
|
|
|
} catch (Exception e) {
|
2017-01-31 01:10:01 +01:00
|
|
|
|
ASF.ArchiLogger.LogGenericException(e);
|
2016-07-28 18:27:01 +02:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2016-01-03 20:36:13 +01:00
|
|
|
|
ServiceHost = null;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2017-02-03 09:54:09 +01:00
|
|
|
|
private static Binding GetTargetBinding() {
|
|
|
|
|
|
Binding result;
|
2017-02-03 09:59:02 +01:00
|
|
|
|
switch (Program.GlobalConfig.WCFBinding) {
|
|
|
|
|
|
case GlobalConfig.EWCFBinding.NetTcp:
|
2017-02-03 09:54:09 +01:00
|
|
|
|
result = new NetTcpBinding {
|
|
|
|
|
|
// We use SecurityMode.None for Mono compatibility
|
|
|
|
|
|
// Yes, also on Windows, for Mono<->Windows communication
|
|
|
|
|
|
Security = { Mode = SecurityMode.None }
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
break;
|
2017-02-03 09:59:02 +01:00
|
|
|
|
case GlobalConfig.EWCFBinding.BasicHttp:
|
2017-02-03 09:54:09 +01:00
|
|
|
|
result = new BasicHttpBinding();
|
|
|
|
|
|
break;
|
2017-02-03 09:59:02 +01:00
|
|
|
|
case GlobalConfig.EWCFBinding.WSHttp:
|
2017-02-03 09:54:09 +01:00
|
|
|
|
result = new WSHttpBinding {
|
|
|
|
|
|
// We use SecurityMode.None for Mono compatibility
|
|
|
|
|
|
// Yes, also on Windows, for Mono<->Windows communication
|
|
|
|
|
|
Security = { Mode = SecurityMode.None }
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
break;
|
|
|
|
|
|
default:
|
2017-02-03 09:59:02 +01:00
|
|
|
|
ASF.ArchiLogger.LogGenericWarning(string.Format(Strings.WarningUnknownValuePleaseReport, nameof(Program.GlobalConfig.WCFBinding), Program.GlobalConfig.WCFBinding));
|
|
|
|
|
|
goto case GlobalConfig.EWCFBinding.NetTcp;
|
2017-02-03 09:54:09 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
result.SendTimeout = new TimeSpan(0, 0, Program.GlobalConfig.ConnectionTimeout);
|
|
|
|
|
|
return result;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2017-02-03 09:56:18 +01:00
|
|
|
|
private static string GetUrlFromBinding(Binding binding) {
|
|
|
|
|
|
if (binding != null) {
|
|
|
|
|
|
return binding.Scheme + "://" + Program.GlobalConfig.WCFHost + ":" + Program.GlobalConfig.WCFPort + "/ASF";
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
ASF.ArchiLogger.LogNullError(nameof(binding));
|
|
|
|
|
|
return null;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2016-08-13 04:39:17 +02:00
|
|
|
|
private void StopClient() {
|
|
|
|
|
|
if (Client == null) {
|
|
|
|
|
|
return;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
if (Client.State != CommunicationState.Closed) {
|
|
|
|
|
|
Client.Close();
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
Client = null;
|
|
|
|
|
|
}
|
2016-01-03 22:23:30 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
2016-08-01 18:17:51 +02:00
|
|
|
|
internal sealed class Client : ClientBase<IWCF> {
|
2016-11-06 17:08:44 +01:00
|
|
|
|
internal Client(Binding binding, EndpointAddress address) : base(binding, address) { }
|
2016-01-03 22:23:30 +01:00
|
|
|
|
|
2016-08-01 18:17:51 +02:00
|
|
|
|
internal string HandleCommand(string input) {
|
2016-05-30 01:57:06 +02:00
|
|
|
|
if (string.IsNullOrEmpty(input)) {
|
2017-01-31 01:10:01 +01:00
|
|
|
|
ASF.ArchiLogger.LogNullError(nameof(input));
|
2016-05-30 01:57:06 +02:00
|
|
|
|
return null;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2016-01-04 00:02:18 +01:00
|
|
|
|
try {
|
2016-07-30 12:17:01 +08:00
|
|
|
|
return Channel.HandleCommand(input);
|
2016-01-04 00:02:18 +01:00
|
|
|
|
} catch (Exception e) {
|
2017-01-31 01:10:01 +01:00
|
|
|
|
ASF.ArchiLogger.LogGenericException(e);
|
2016-01-04 00:02:18 +01:00
|
|
|
|
return null;
|
|
|
|
|
|
}
|
2016-01-03 20:36:13 +01:00
|
|
|
|
}
|
|
|
|
|
|
}
|
2016-11-24 07:32:16 +01:00
|
|
|
|
}
|