In my state, it isn’t illegal per se, but it is against the law to high-beam the oncoming traffic. That’s how they get ya.
Solution...make sure your lights are on low beam, and flick the switch a few times.
At night, unless you’re willing to turn your lights OFF for a split second (probably also illegal), you’re SOL.
You can't flash to tell an oncoming car to dim their lights?