Skip to content

Commit

Permalink
X2chUAヘッダ追加とnonce値のms単位の制御
Browse files Browse the repository at this point in the history
  • Loading branch information
onihusube committed Apr 11, 2022
1 parent bc7ed6b commit b74a73b
Show file tree
Hide file tree
Showing 5 changed files with 76 additions and 13 deletions.
9 changes: 8 additions & 1 deletion 2chAPIProxy/AppSetting.cs
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,8 @@ public class AppSetting
private bool enablePostv2 = true;
private bool enablePostv2onPink = false;
private bool enableUTF8Post = false;
private bool addX2chUAHeader = false;
private bool addX2chUAHeader = true;
private bool addMsToNonce = true;
String _HMkey = "hO2QHdapzbqbTFOaJgZTKXgT2gWqYS";
String _Appkey = "JYW2J6wh9z8p8xjGFxO3M2JppGCyjQ";
String _UserAgent0 = "";
Expand Down Expand Up @@ -358,6 +359,12 @@ public bool AddX2chUAHeader
set { addX2chUAHeader = value; change = true; }
}

public bool AddMsToNonce
{
get => addMsToNonce;
set { addMsToNonce = value; change = true; }
}


public String CryptData(String data, bool encrypt = true)
{
Expand Down
12 changes: 9 additions & 3 deletions 2chAPIProxy/DatProxy.cs
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,8 @@ public class DatProxy
public bool EnablePostv2onPink { get; set; }
public bool EnableUTF8Post { get; set; }
public bool AddX2chUAHeader { get; set; }
public bool AddMsToNonce { get; set; }


private string[] PostFieldOrederArray;

Expand Down Expand Up @@ -982,6 +984,7 @@ private string Monakey
public void ResetMonakey()
{
Monakey = "";
ViewModel.OnModelNotice("MonaKeyをリセットしました。");
}

//private string Monakey = "7b6799cc2bb1eef3acadffeecc180df6d1c7caab887326120056660f6ac05b45";
Expand Down Expand Up @@ -1142,7 +1145,11 @@ private void ResPostv2(Session oSession, bool is2ch)

// nonceの取得
//string nonce = string.Format("{0}.{1:000}", (ulong)DateTime.UtcNow.Subtract(new DateTime(1970, 1, 1)).TotalSeconds, DateTime.UtcNow.Millisecond);
string nonce = string.Format("{0}.{1:000}", post_field_map["time"], DateTime.UtcNow.Millisecond);
string nonce = AddMsToNonce switch
{
true => string.Format("{0}.{1:000}", post_field_map["time"], DateTime.UtcNow.Millisecond),
false => post_field_map["time"].ToString()
};

// 各種値の計算とヘッダセット
Write.Headers.Add("X-PostSig", CreatePostsignature(post_field_map, nonce, UA, dst_encoding));
Expand Down Expand Up @@ -1324,8 +1331,7 @@ private void ResPostv2(Session oSession, bool is2ch)
// E3000番台のエラーが帰ってきたらMonaKeyを更新する(雑な暫定対応
if (wres.Headers["X-Chx-Error"].Contains("E3331") == false && wres.Headers["X-Chx-Error"].Contains("E3"))
{
Monakey = "";
ViewModel.OnModelNotice("MonaKeyをリセットしました。");
ResetMonakey();
}

}
Expand Down
42 changes: 34 additions & 8 deletions 2chAPIProxy/MainWindow.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" xmlns:local="clr-namespace:_2chAPIProxy" mc:Ignorable="d" x:Class="_2chAPIProxy.MainWindow"
Title="2chAPIProxy Ver:2022.04.10 test" Height="480" Width="640"
Title="2chAPIProxy Ver:2022.04.12 test" Height="480" Width="640"
Icon="icon3.ico" WindowStartupLocation="CenterScreen" ResizeMode="CanMinimize" Background="White" d:DataContext="{d:DesignInstance {x:Type local:ViewModel}}">
<Window.InputBindings>
<KeyBinding Gesture="Ctrl+S" Command="{Binding SaveSetting}"/>
Expand Down Expand Up @@ -311,8 +311,8 @@
</Style>
</CheckBox.Style>
</CheckBox>
<CheckBox x:Name="PostNoReplace_Copy" Content="書き込みへの関与を最小限にする" Height="20" Margin="398,156,42,0" VerticalAlignment="Top" ToolTip="書き込み(スレ立て)時、5chへの変更、https化、UAの変更、のみを行います。送信用にプロクシを通さず書き込むのとほぼ同じになります。" IsChecked="{Binding PostNoReplace, Mode=TwoWay, NotifyOnTargetUpdated=True, NotifyOnSourceUpdated=True, UpdateSourceTrigger=PropertyChanged}" TabIndex="5"/>
<CheckBox x:Name="SetReferrer_Copy" Content="リファラを設定する" Height="20" Margin="233,156,282,0" VerticalAlignment="Top" ToolTip="書き込み時のRefererヘッダに、書き込むスレのURLを常に設定するようにします" IsChecked="{Binding SetReferrer, Mode=TwoWay, NotifyOnTargetUpdated=True, NotifyOnSourceUpdated=True, UpdateSourceTrigger=PropertyChanged}" TabIndex="4">
<CheckBox x:Name="PostNoReplace_Copy" Content="書き込みへの関与を最小限にする" Height="20" Margin="410,156,30,0" VerticalAlignment="Top" ToolTip="書き込み(スレ立て)時、5chへの変更、https化、UAの変更、のみを行います。送信用にプロクシを通さず書き込むのとほぼ同じになります。" IsChecked="{Binding PostNoReplace, Mode=TwoWay, NotifyOnTargetUpdated=True, NotifyOnSourceUpdated=True, UpdateSourceTrigger=PropertyChanged}" TabIndex="5"/>
<CheckBox x:Name="SetReferrer_Copy" Content="リファラを設定する" Height="20" Margin="237,156,278,0" VerticalAlignment="Top" ToolTip="書き込み時のRefererヘッダに、書き込むスレのURLを常に設定するようにします" IsChecked="{Binding SetReferrer, Mode=TwoWay, NotifyOnTargetUpdated=True, NotifyOnSourceUpdated=True, UpdateSourceTrigger=PropertyChanged}" TabIndex="4">
<CheckBox.Style>
<Style TargetType="{x:Type CheckBox}">
<Setter Property="FontSize" Value="12px"/>
Expand All @@ -325,9 +325,9 @@
</Style>
</CheckBox.Style>
</CheckBox>
<GroupBox x:Name="Postv2Setting" Header="新書き込み仕様関連" HorizontalAlignment="Left" Height="125" Margin="10,206,0,0" VerticalAlignment="Top" Width="604"/>
<GroupBox x:Name="Postv2Setting" Header="新書き込み仕様関連" HorizontalAlignment="Left" Height="172" Margin="10,206,0,0" VerticalAlignment="Top" Width="604"/>
<CheckBox x:Name="EnablePostv2" Content="新書き込み方式を有効にする" VerticalAlignment="Top" Margin="33,242,428,0" Height="20" ToolTip="2022年4月頃導入の新しい書き込み方式を使用します" TabIndex="3" IsChecked="{Binding EnablePostv2, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"/>
<CheckBox x:Name="EnablePostv2Pink" Content="bbspinkでも有効にする" VerticalAlignment="Top" Margin="233,242,257,0" Height="20" ToolTip="新しい書き込み方式をbbspinkでも使用します" TabIndex="3" IsChecked="{Binding EnablePostv2onPink, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}">
<CheckBox x:Name="EnablePostv2Pink" Content="bbspinkでも有効にする" VerticalAlignment="Top" Margin="237,242,253,0" Height="20" ToolTip="新しい書き込み方式をbbspinkでも使用します" TabIndex="3" IsChecked="{Binding EnablePostv2onPink, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}">
<CheckBox.Style>
<Style TargetType="{x:Type CheckBox}">
<Setter Property="FontSize" Value="12px"/>
Expand All @@ -340,7 +340,7 @@
</Style>
</CheckBox.Style>
</CheckBox>
<CheckBox x:Name="PostUTF8" Content="UTF-8で書き込む" VerticalAlignment="Top" Margin="398,242,107,0" Height="20" ToolTip="書き込み時に元のテキストデータをUTF-8エンコーディングに変換して書き込みます" TabIndex="3" IsChecked="{Binding EnableUTF8Post, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}">
<CheckBox x:Name="PostUTF8" Content="UTF-8で書き込む" VerticalAlignment="Top" Margin="410,242,95,0" Height="20" ToolTip="書き込み時に元のテキストデータをUTF-8エンコーディングに変換して書き込みます" TabIndex="3" IsChecked="{Binding EnableUTF8Post, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}">
<CheckBox.Style>
<Style TargetType="{x:Type CheckBox}">
<Setter Property="FontSize" Value="12px"/>
Expand All @@ -361,9 +361,35 @@
</Popup>
</StackPanel>
</Button>
<TextBox x:Name="field_order" HorizontalAlignment="Left" Height="25" Margin="104,278,0,0" TextWrapping="NoWrap" VerticalAlignment="Top" Width="480" ToolTip="書き込み(スレ立て)時に送信するデータの順序を指定します。&区切りで左から右に並べてください。無いものは末尾に追加されます。" Text="{Binding PostFieldOrder, Mode=TwoWay, NotifyOnTargetUpdated=True, NotifyOnSourceUpdated=True, UpdateSourceTrigger=LostFocus}" TabIndex="1" IsEnabled="{Binding EnablePostv2, Mode=OneWay}"/>
<TextBlock HorizontalAlignment="Left" TextWrapping="Wrap" Text="フィールド順序" VerticalAlignment="Top" Margin="30,283,0,0" Width="69" Height="20"/>
<TextBox x:Name="field_order" HorizontalAlignment="Left" Height="25" Margin="104,339,0,0" TextWrapping="NoWrap" VerticalAlignment="Top" Width="480" ToolTip="書き込み(スレ立て)時に送信するデータの順序を指定します。&区切りで左から右に並べてください。無いものは末尾に追加されます。" Text="{Binding PostFieldOrder, Mode=TwoWay, NotifyOnTargetUpdated=True, NotifyOnSourceUpdated=True, UpdateSourceTrigger=LostFocus}" TabIndex="1" IsEnabled="{Binding EnablePostv2, Mode=OneWay}"/>
<TextBlock HorizontalAlignment="Left" TextWrapping="Wrap" Text="フィールド順序" VerticalAlignment="Top" Margin="30,344,0,0" Width="69" Height="20"/>
<CheckBox x:Name="PostRoninInvalid" Content="書き込み時に浪人を無効化する" HorizontalAlignment="Left" Height="20" Margin="33,156,0,0" VerticalAlignment="Top" IsChecked="{Binding PostRoninInvalid, Mode=TwoWay, NotifyOnTargetUpdated=True, NotifyOnSourceUpdated=True, UpdateSourceTrigger=PropertyChanged}" TabIndex="12" ToolTip="書き込み(スレ立て)時に浪人関連の情報を削除して書き込みます。書き込みに浪人を利用したくない場合にご利用ください。"/>
<CheckBox x:Name="AddX2chUAHeader" Content="X-2ch-UAヘッダを送信する" VerticalAlignment="Top" Margin="33,267,428,0" Height="20" ToolTip="新しい書き込み方式をbbspinkでも使用します" TabIndex="3" IsChecked="{Binding AddX2chUAHeader, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}">
<CheckBox.Style>
<Style TargetType="{x:Type CheckBox}">
<Setter Property="FontSize" Value="12px"/>
<Setter Property="IsEnabled" Value="True"/>
<Style.Triggers>
<DataTrigger Binding="{Binding EnablePostv2}" Value="False">
<Setter Property="IsEnabled" Value="False"/>
</DataTrigger>
</Style.Triggers>
</Style>
</CheckBox.Style>
</CheckBox>
<CheckBox x:Name="AddMsToNonce" Content="nonce値にミリ秒を付加する" VerticalAlignment="Top" Margin="237,267,232,0" Height="20" ToolTip="新しい書き込み方式をbbspinkでも使用します" TabIndex="3" IsChecked="{Binding AddMsToNonce, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}">
<CheckBox.Style>
<Style TargetType="{x:Type CheckBox}">
<Setter Property="FontSize" Value="12px"/>
<Setter Property="IsEnabled" Value="True"/>
<Style.Triggers>
<DataTrigger Binding="{Binding EnablePostv2}" Value="False">
<Setter Property="IsEnabled" Value="False"/>
</DataTrigger>
</Style.Triggers>
</Style>
</CheckBox.Style>
</CheckBox>
</Grid>
</TabItem>
</TabControl>
Expand Down
19 changes: 19 additions & 0 deletions 2chAPIProxy/ViewModels/ViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,8 @@ public ViewModelImpl()
enablePostv2onPink = Setting.EnablePostv2onPink;
enableUTF8Post = Setting.EnableUTF8Post;
postFieldOrder = Setting.PostFieldOrder;
addX2chUAHeader = Setting.AddX2chUAHeader;
addMsToNonce = Setting.AddMsToNonce;

//スリープ/休止状態時の処理
Microsoft.Win32.SystemEvents.PowerModeChanged += new Microsoft.Win32.PowerModeChangedEventHandler(PowermodeChanged);
Expand Down Expand Up @@ -199,6 +201,8 @@ public void Init()
DatProxy.EnablePostv2onPink = EnablePostv2onPink;
DatProxy.EnableUTF8Post = EnableUTF8Post;
DatProxy.PostFieldOrder = PostFieldOrder;
DatProxy.AddX2chUAHeader = AddX2chUAHeader;
DatProxy.AddMsToNonce = AddMsToNonce;

//設定の適用、APIアクセスクラス
DatProxy.APIMediator.AppKey = this.Appkey;
Expand Down Expand Up @@ -878,6 +882,21 @@ public bool AddX2chUAHeader
}
}

private bool addMsToNonce;

public bool AddMsToNonce
{
get => addMsToNonce;
set
{
if (addMsToNonce != value)
{
Setting.AddMsToNonce = DatProxy.AddMsToNonce = addMsToNonce = value;
NoticePropertyChanged("AddX2chUAHeader");
}
}
}

int _PortNumber;
public int PortNumber
{
Expand Down
7 changes: 6 additions & 1 deletion APIMediator/APIAccess.cs
Original file line number Diff line number Diff line change
Expand Up @@ -193,8 +193,13 @@ public void UpdateSID()
}
APIRequest.Method = "POST";
APIRequest.UserAgent = this.SidUA;

// VM(UIからの入力直後)に"X-2ch-UA : "を取り除くのでここでは考慮しない
APIRequest.Headers.Add("X-2ch-UA", this.X2chUA);
//APIRequest.Headers.Add("X-2ch-UA", this.X2chUA);
// しばらく以前の振る舞いを維持しておく
var res = this.X2chUA.Split(':');
APIRequest.Headers.Add("X-2ch-UA", (res.Length == 1) ? (this.X2chUA) : (res[1]));

APIRequest.Timeout = GetSIDTimeout;
APIRequest.ContentType = "application/x-www-form-urlencoded";
APIRequest.ServicePoint.Expect100Continue = false;
Expand Down

0 comments on commit b74a73b

Please sign in to comment.